Python:为什么我的googlewebscraping代码没有使用beautifulsoup返回搜索结果?

2024-10-02 12:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个python脚本来显示google中针对给定搜索查询的前5个结果的链接

我使用的是beautiful soup,在检查了google的html之后,我发现可以在标签div^{cl1}中找到搜索结果链接$

import bs4, requests

mySearch=input()
address='http://www.google.com/search?q='+mySearch
googleRes=requests.get(address)

googleSoup=bs4.BeautifulSoup(googleRes.text)
linkBlocks=googleSoup.select('div.r a')

但是,列表linkBlocks为空,而不是用搜索结果链接填充。如何将搜索结果链接到linkBlocks列表中


Tags: div脚本列表链接addresshtmlgooglerequests
1条回答
网友
1楼 · 发布于 2024-10-02 12:23:35

使用User-Agent

import bs4, requests
headers = {'User-Agent':
       'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
mySearch="beautifulsoup"
address='http://www.google.com/search?q='+mySearch
googleRes=requests.get(address,headers=headers)
googleSoup=bs4.BeautifulSoup(googleRes.text,'html.parser')
linkBlocks=googleSoup.select('div.r a')
print(linkBlocks)

相关问题 更多 >

    热门问题