Googleイメージローラー
3566 ワード
イメージスクロール-Google
Seleniumを使用したWebページのスライス
fdef infinite_scroll_down(driver, wait=.5):
body = driver.find_element_by_tag_name('body')
# more result btn
while driver.find_element(By.XPATH, '//input[@class="mye4qd"]').is_displayed() is False:
body.send_keys(Keys.END)
time.sleep(wait)
driver.find_element(By.XPATH, '//input[@class="mye4qd"]').click()
while driver.find_element(By.XPATH, '//div[@class="OuJzKb Bqq24e"]').text != '더 이상 표시할 콘텐츠가 없습니다.':
body.send_keys(Keys.END)
time.sleep(wait)
body.send_keys(Keys.END)
[Google]サムネイル画像の取得get_attrubute()
によっては、elementの状態が異なる可能性があるため、重複検査が実現される.def get_thumbnail_urls(driver, wait=.1, retry=3):
imgs = driver.find_elements(By.XPATH, '//img[@class="rg_i Q4LuWd"]')
urls = []
for img in imgs:
count = retry
while count:
src = img.get_attribute('src')
if src is None:
src = img.get_attribute('data-src')
if src is not None:
urls += [src]
break
count -= 1
time.sleep(wait)
print(f'{len(urls)}/{len(imgs)} images are loaded')
return urls
[Google]オリジナル画像のインポートdef get_image_urls(driver, wait=.1, retry=3):
def check_loaded():
loading_bar = driver.find_element(By.XPATH,
'//div[@class="tvh9oe BIB1wf"]//div[@class="k7O2sd"]')
return loading_bar.get_attribute('style') == 'display: none;'
body = driver.find_element_by_tag_name('body')
body.send_keys(Keys.HOME)
imgs = driver.find_elements_by_xpath('//img[@class="rg_i Q4LuWd"]')
urls = []
for n, img in enumerate(imgs):
if n == 0:
img.click()
count = retry
while count and not check_loaded():
time.sleep(wait)
count -= 1
if check_loaded():
xpath = '//div[@class="tvh9oe BIB1wf"]//img[@class="n3VNCb"]'
urls.append(driver.find_element_by_xpath(xpath).get_attribute('src'))
driver.find_element_by_xpath('//div[@class="tvh9oe BIB1wf"]//a[@class="gvi3cf"]').click()
print(f'{len(urls)}/{len(imgs)} images are loaded')
return urls
[Google] ToDoReference
この問題について(Googleイメージローラー), 我々は、より多くの情報をここで見つけました https://velog.io/@msjeong/google-image-crawlerテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol