无法使用Selenium Python点击Google Trends网站的探索文本
我需要获取过去30天和过去12个月的Google趋势数据,用于我现在的项目。我可以通过Google Trends API获取每日的数据。这个包里有一个叫做InterestsOverTime的API,但当我试图使用这个API访问探索页面时,谷歌会阻止任何自动化的尝试,包括网页爬虫或使用Selenium。
所以我决定直接去主页面,然后点击探索页面,但我用Selenium实现这一点时失败了。我对Selenium非常陌生,所以请帮帮我。
这是我正在使用的代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ES
chrome_options = Options()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options, keep_alive=True)
url = "https://trends.google.com/trends/"
driver.get(url)
explore_button = WebDriverWait(driver,10).until(
ES.visibility_of_element_located((By.XPATH,"//span[contains(text(),'Explore')]"))
)
explore_button.click()
我尝试了多种方法来访问,包括使用Google Trends API的封装API,以及尝试用Python的网页爬虫和Selenium访问探索页面,但都没有成功。即使是尝试用Selenium点击“探索”这个文本也没有效果。
2 个回答
0
这个问题出在这个XPath表达式上://span[contains(text(),'Explore')]
第一个问题是,它找到了3个网页元素。第二个问题是,你应该找的是button
元素,而不是span
元素。
只需要把XPath表达式改成下面这样:
(//span[text()='Explore']//parent::button)[1]
关于XPath的解释:
//span[text()='Explore']
:这个部分的意思是选择文档中任何文本内容完全是“Explore”的<span>
元素。//parent::button
:这个部分选择刚才选中的<span>
元素的父元素,但前提是这个父元素是<button>
元素。[1]
:最后的[1]
表示如果有多个符合条件的元素,只取第一个。
1
通过使用xpath来唯一识别按钮元素。
//button[contains(@jsaction,'clickmod')][.//span[contains(text(),'Explore')]]
代码:
explore_button = WebDriverWait(driver,10).until(
ES.visibility_of_element_located((By.XPATH,"//button[contains(@jsaction,'clickmod')][.//span[contains(text(),'Explore')]]"))
)
explore_button.click()