首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >抓取Google Trending Stories页面(Python)

抓取Google Trending Stories页面(Python)
EN

Stack Overflow用户
提问于 2015-09-10 04:57:32
回答 2查看 123关注 0票数 0

我想把谷歌热门故事页面上所有排名前50的搜索结果都剔除出来:https://www.google.com/trends/

然而,当我运行下面的代码时,没有出现任何搜索结果,看起来我只得到了标签:

代码语言:javascript
复制
wikis = ["https://www.google.com/trends/"]
for wiki in wikis:
    website = requests.get(wiki)
    soup = BeautifulSoup(website.content, "lxml")
    text = ''.join([element.text for element in soup.body.find_all(lambda tag:    tag != 'script', recursive=False)])
    new =  re.sub(r'[^a-zA-Z \n]','',text)

输出:

代码语言:javascript
复制
MyAccountSearchMapsYouTubePlayNewsGmailDriveCalendarTranslatePhotosMoreShoppingWalletFinanceDocsBooksBloggerContactsEven more from GoogleSign inYou are using unsupported browser Some features may not work correctly Upgrade to a modern browser such as Google ChromeTrends has upgraded to a newer version which is not supported by this devicedismiss

有什么帮助吗?

EN

回答 2

Stack Overflow用户

发布于 2015-09-10 05:33:05

你的问题是https://www.google.com/trends/是由Javascript生成的。所以你不能使用requests,因为它是一个http库。

要在禁用Javascript的情况下在浏览器中测试网站: firefox示例:

代码语言:javascript
复制
about:config

javascript.enabled = false

请查看google/SO以查找支持Javascript的库

票数 0
EN

Stack Overflow用户

发布于 2021-09-21 16:39:42

  1. 打开开发工具网络选项卡。

  1. 单击Fetch/XHR。

在我的例子中,它是:topdailytrends?hl=en-US&tz=-180&geo=UA.

  1. Find Name with the response with the response(单击某个名称并点击预览)。

单击该名称后,您将在Headers选项卡中看到

  1. 指向该requests.get(),在我的示例中,它是:https://trends.google.com/trends/api/topdailytrends?hl=en-US&tz=-180&geo=UA.

示例代码:

代码语言:javascript
复制
import requests, json

response = requests.get('https://trends.google.com/trends/api/topdailytrends?hl=en-US&tz=-180&geo=UA').text.replace(")]}',", "").strip()

json_data = json.loads(response)
trending_searches = json_data['default']['trendingSearches']

print(json.dumps(trending_searches, indent=2, ensure_ascii=False))

----------
'''
[
  {
    "title": "З днем Ангела Марії",
    "formattedTraffic": "10K+",
    "trendingSearchUrl": "/trends/trendingsearches/daily?geo=UA#%D0%97%20%D0%B4%D0%BD%D0%B5%D0%BC%20%D0%90%D0%BD%D0%B3%D0%B5%D0%BB%D0%B0%20%D0%9C%D0%B0%D1%80%D1%96%D1%97",
    "country": "Ukraine"
  }
...
]
'''
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32489042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档