BeautifulSoup google 趋势

BeautifulSoup google trend

我是编程界的新手,刚刚了解了 Python 的基础知识。我刚刚开始练习网络爬虫并且已经遇到了问题。我使用 BeautifulSoup.

编写了一个非常简单的代码
from bs4 import BeautifulSoup
from urllib.request import urlopen

response = urlopen('https://trends.google.com/trends/?geo=US/')
soup = BeautifulSoup(response, 'html.parser')
for anchor in soup.select(".list-item-title"):
    print(anchor)

我想检索最近热搜的故事的名字;但是,上面的代码没有按预期运行,returns 一片空白。

如果有人能指出错误,我将不胜感激。谢谢!

Google 趋势 (url) 是动态的,意味着数据由 JavaScript 生成,BeautifulSoup 无法呈现 javaSceipt.So,您需要自动化工具像带有 BeautifulSoup` 的硒。请 运行 代码。

脚本:

from bs4 import BeautifulSoup
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

url = 'https://trends.google.com/trends/?geo=US/'

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
time.sleep(8)
driver.get(url)
time.sleep(10)

soup = BeautifulSoup(driver.page_source, 'html.parser')
#driver.close()

for anchor in soup.select(".list-item-title"):
    print(anchor.text)

输出:

Meta
Lupus
Liga Europy
Chelsea
Prova do lider
Masks in Kenya
UANL
Jussie Smollett
ישי ריבו
Winter storm warning