【发布时间】:2021-05-20 18:53:21
【问题描述】:
我正在尝试使用 selenium (Python) 从 Google 搜索结果中抓取标题和链接。我的问题是我只能抓取前 4 个结果,而不能抓取其他 6 个结果。在这里,结果只是空的。我的感觉是,这可能与网页的加载时间有关,但我不确定。我一直在考虑实现wait.until(EC.visibility_of_element_located 语句,但还没有找到使它起作用的方法。
有人在这个问题上有经验吗?非常感谢!
代码:
import urllib
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
root = "https://www.google.com/"
url = "https://google.com/search?q="
query = 'Why do I only see the first 4 results?' # Fill in google query
query = urllib.parse.quote_plus(query)
link = url + query
print(f'Main link to search for: {link}')
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome(options=options)
driver.get(link)
WebDriverWait(driver, 10)
headings = driver.find_elements_by_xpath('//div[@class = "g"]') #Heading elements
for heading in headings:
title = heading.find_elements_by_tag_name('h3')
links = heading.get_attribute('href') # This ain't working either, any help?
print(links)
#link = heading.find_element_by_name('a href')
for t in title:
print('title:', t.text)
【问题讨论】:
标签: python selenium selenium-webdriver