前提・実現したいこと
https://qiita.com/tigerz55/items/e4d1d5b8e00f7a771177
上記のURLに乗っているコードを実装中に以下のエラーメッセージが発生しました。
解決方法を教えていただけると幸いです。
発生している問題・エラーメッセージ
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"body > main > ul > ul > li:nth-child(2) > a"} (Session info: chrome=70.0.3538.102) (Driver info: chromedriver=2.43.600229 (3fae4d0cda5334b4f533bede5a4787f7b832d052),platform=Mac OS X 10.14.0 x86_64)
該当のソースコード
python(jupyter
1import cv2 2import urllib.request as req 3from selenium import webdriver 4import os 5import time 6 7driver = webdriver.Chrome(executable_path='/Users/a/Downloads/chromedriver') 8 9count=1 10 11#対象URLの指定 12driver.get("https://keyaki.foxtrotalphalima.com/blog/nogi?id=all&vw=1&p=0") 13time.sleep(5) 14 15while True: 16 #ブログリスト取得 17 blog_list = driver.find_elements_by_class_name('blog') 18 for blog in blog_list: 19 #画像リスト取得 20 images = blog.find_elements_by_css_selector('ul > li') 21 for image in images: 22 #画像のURL取得 23 image_url = image.find_element_by_css_selector('img').get_attribute('src') 24 #画像保存 25 try: 26 image = req.urlopen(image_url).read() 27 if not os.path.exists('./image/nogi'): 28 os.mkdir('./image/nogi') 29 with open('./image/nogi/'+str(count)+'.jpg',mode='wb') as f: 30 f.write(image) 31 print('download - {}'.format(count)) 32 except: 33 print('cant open url') 34 count += 1 35 36 if count > 3500: 37 38 break 39 #次ページのURLを取得し移動 40 print('------------------- go to next page --------------------------') 41 next_url = driver.find_element_by_css_selector('body > main > ul > ul > li:nth-child(2) > a').get_attribute('href') 42 driver.get(next_url) 43 time.sleep(5) 44 45driver.quit()

回答1件
あなたの回答
tips
プレビュー