「次へ」「次のページへ」 をPythonのselenium,beutifulsoupで ユーザー名スクレイピングしたいです。
下記の方法で行いまたが、最初の1ページだけユーザー名スクレイピングされます。
次のページ行ってもスクレイピングするにはどのようにすればよろしいでしょうか。
HTML 「次へ」ボタン
<button tabindex="-1" class="btn-paging next" ng-click="toPage(pageStatus.current + 1);" ng-disabled="pageStatus.current === pageStatus.last"><i class="icon-angle-right"></i></button>
こちらの部分で全てのユーザーを取得しています。
こちらを修正する必要ありますか。
time.sleep(4)
#すべてのユーザーを取得
users_names = [i.get_text() for i in soup.find_all(class_="fn-user ellip-1 ng-binding")]
print(users_names)
全体のCode
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
#from datetime import datetime as dt, date, timedelta
import pyautogui
import time
from datetime import datetime as dt, date, timedelta
#headless background
option = Options()
#backgrand
#option.add_argument('--headless')
#ログイン情報を維持するための設定
# 参考→https://rabbitfoot.xyz/selenium-chrome-profile/
PROFILE_PATH ="C:\\Users\\example\\AppData\\Local\\Google\\Chrome\\User Data\\" # 変更
option.add_argument('--user-data-dir=' + PROFILE_PATH)
option.add_argument('--profile-directory=Default')
# ブラウザを開く。 #options=option background
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install() ,options=option)
#Getting Default Adapter failed error message
option.add_experimental_option('excludeSwitches', ['enable-logging'])
strDate = dt.now().strftime("%Y%m%d")
#指定したURLに遷移
URL= "https://example.com/apt/1/"
# Googleの検索TOP画面を開く。
driver.get(URL)
#60秒待機
time.sleep(10)
#menuボタンをクリック
menu = driver.find_element_by_xpath('//*[@id="mainContainerWrapper"]/member-control/section[2]/div[2]/div/ul/li[1]/div/div/ul/li[2]/span')
menu.click()
#1秒待機
time.sleep(2)
#BeautifulSoup指定
html = driver.page_source.encode('utf-8')
soup = BeautifulSoup(html, 'html.parser')
#ユーザ数取得
member=soup.find(class_="member-count ng-binding").get_text()
print(member)
#ループのページ数取得
loop_num=math.ceil(int(member)/100)
print(loop_num)
for i in range(1,loop_num):
#次のボタンクリック
next_button = driver.find_element_by_xpath('//*[@id="mainContainerWrapper"]/member-control/div/div/button[3]').click()
time.sleep(4)
#すべてのユーザーを取得
users_names = [i.get_text() for i in soup.find_all(class_="fn-user ellip-1 ng-binding")]
print(users_names)
もし分かる方がいましたら、教えていただけると幸いです。