行いたい内容
現在、BeautifulSoupとseleniumを用いてスクレイピング行っています。
1.一覧ページのリンクをクリック
2.個別ページを開いて要素取得
3.一覧に戻って次のリンクをクリック
4.個別ページを開いて要素取得
上記のように大枠としては動かしたいのですが、思うように要素が取得出来ませんので、お力添え頂けますと幸いです。
参考サイト
https://qiita.com/Gen6/items/66f190d3c9e345e97ed2
処理の流れ
1.beautiful soup の.find_elements_by_xpathに渡す文字列を記述
2.以下の内容を10回繰り返す
3.beautiful soup の.find_elements_by_xpathでリンクをクリック
4.for page_title in bs.find_all('meta', attrs={'property': 'og:title'}):でタイトル要素取得
5.for script_ele in bs.find_all ( "script" ):で<script>内のイメージurlを全て抽出
6.blanks = ['' for _ in range(1,len(image_url))]でimageの数だけ空白を追加
7.ウィンドウをバックして一覧にもどる
8.カウントに1を加え次の要素をクリック
問題点
上記の様に行いたいのですが、
for page_title in bs.find_all('meta', attrs={'property': 'og:title'}):
と
image_urlが出力されません。
記述しているコード
python
1import urllib.request 2import pandas as pd 3from bs4 import BeautifulSoup 4from selenium import webdriver 5from selenium.common.exceptions import NoSuchElementException 6import time 7driver = webdriver.Chrome ( executable_path=r'C:\Users\chromedriver.exe' ) 8driver.get ("https://hogehoge.com/" ) 9 10html = driver.page_source 11bs = BeautifulSoup ( html, "html.parser" ) 12time.sleep ( 5 ) 13 14 15for i in range(1): 16 try: 17 name_list = [] 18 19 path_front = '//*[@id="j-wishlist-container"]/div/div[1]/div[' 20 count = 2 21 path_end = ']/div/div[1]' 22 23 for _ in range(10): 24 path = path_front + str(count) + path_end 25 for l in driver.find_elements_by_xpath(path): 26 l.click() 27 try: 28 driver.find_element_by_class_name('next-dialog-close').click() 29 except: 30 pass 31 break 32 break 33 for page_title in bs.find_all('meta', attrs={'property': 'og:title'}): 34 print (page_title['content']) 35 for script_ele in bs.find_all ( "script" ): 36 text_list = str(script_ele).split("\"")# 引用符「"」で分割 37 image_url = [] 38 for img_url in text_list: 39 if img_url.startswith("http") and img_url.endswith("jpg"): 40 image_url.append(img_url) 41 blanks = ['' for _ in range(1,len(image_url))] 42 print (image_url) 43 break 44 break 45 break 46 break 47 driver.back() 48 count += 1 49 for name_title in zip(name_list): 50 print (name_title, "\n+++++++++++++++++++++++++++++++++++++++++++++++++++") 51 l.click() 52 except: 53 pass
for文の使い方に問題があるのではないかと思っています。
ちなみに、順にクリックするという構文をなくし、
python
1import urllib.request 2import pandas as pd 3from bs4 import BeautifulSoup 4from selenium import webdriver 5from selenium.common.exceptions import NoSuchElementException 6import time 7driver = webdriver.Chrome ( executable_path=r'C:\Users\chromedriver.exe' ) 8driver.get ("https://hogehoge.com/個別ページ" ) 9 10html = driver.page_source 11bs = BeautifulSoup ( html, "html.parser" ) 12time.sleep ( 5 ) 13 14 15for page_title in bs.find_all('meta', attrs={'property': 'og:title'}): 16 print (page_title['content']) 17 for script_ele in bs.find_all ( "script" ): 18 text_list = str(script_ele).split("\"")# 引用符「"」で分割 19 image_url = [] 20 for img_url in text_list: 21 if img_url.startswith("http") and img_url.endswith("jpg"): 22 image_url.append(img_url) 23 blanks = ['' for _ in range(1,len(image_url))] 24 print (image_url) 25 26
では正しく要素が2つとも抽出出来ます。
###追記
記述しているコードで動作させると以下のように関数に値が格納されるのですが、
image_urlとpage_titleが出てきません。
回答1件
あなたの回答
tips
プレビュー