![](/img/trans.png)
[英]How to click on Load More button within Google Trends and print all the titles through Selenium and Python
[英]How to extract titles/text from Google Trends and print them through Selenium and Python
我想從這個網站中提取每行中的不同標題:
https://trends.google.com/trends/trendingsearches/realtime?geo=AR&category=all
我嘗試了一些沒有運氣的嘗試。 我認為通過按類搜索元素我會得到所需的文本:
from selenium import webdriver
driver=webdriver.Chrome('path to bin')
driver.get('https://trends.google.com/trends/trendingsearches/realtime?geo=AR&category=all')
hrefs = driver.find_elements_by_class_name('title')
print hrefs
print(len(hrefs))
driver.quit()
先謝謝你們! 瓊
你真是太近了! 你只需要從標題中獲取文本,試試這個:
from selenium import webdriver
driver=webdriver.Chrome('path to bin')
driver.get('https://trends.google.com/trends/trendingsearches/realtime?geo=AR&category=all')
Titles = driver.find_elements_by_class_name('title')
for title in Titles:
print(title.text)
driver.quit()
@ PixelEinstein的答案將滿足您的要求。 但作為最佳實踐的一部分,您應始終最大化 瀏覽器窗口並誘導WebDriverWait 首先顯示元素,然后按如下方式提取文本:
代碼塊:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument('disable-infobars') driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') driver.get('https://trends.google.com/trends/trendingsearches/realtime?geo=AR&category=all') titles = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='title']"))) for title in titles: print(title.text) driver.quit()
控制台輸出:
Mauricio Macri • Cyst • Pancreas Abortion • National Congress of Argentina • Debate Abortion • Mayra Mendoza • Argentine Chamber of Deputies • Deputy
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.