我要完成的工作是针对Google趋势的一个简单的python网络抓取脚本,并在上课时遇到问题
from bs4 import BeautifulSoup
import requests
results = requests.get("https://trends.google.com/trends/trendingsearches/daily?geo=US")
soup = BeautifulSoup(results.text, 'lxml')
keyword_list = soup.find_all('.details-top')
for keyword in keyword_list:
print(keyword)
在打印 标签 时,我收到并清空类,但是在打印汤时,我收到了整个HTML文档。我的目标是打印出搜索页面https://trends.google.com/trends/trendingsearches/daily?geo=AU
的每个“关键字”的文本这有一个结果列表:
1. covid-19
2.Woolworths jobs
如果您使用Google开发人员选项,请选择“检查”并将鼠标悬停在标题上,您会看到 div.details-top 。
我如何打印每个标题的文本
答案 0 :(得分:2)
我可以在开发工具网络标签中看到从API调用动态检索数据。您可以在该网址上输入xhr,然后在响应文本上使用regex来解析出查询标题。
import requests, re
from bs4 import BeautifulSoup as bs
r = requests.get('https://trends.google.com/trends/api/dailytrends?hl=en-GB&tz=0&geo=AU&ns=15').text
p = re.compile(r'"query":"(.*?)"')
titles = p.findall(r)
print(titles) # 2.7 use print titles