Web scraping 为什么在我的网页抓取程序中什么都没有被解析?

Web scraping 为什么在我的网页抓取程序中什么都没有被解析?,web-scraping,beautifulsoup,python-requests,python-webbrowser,Web Scraping,Beautifulsoup,Python Requests,Python Webbrowser,我做了这个代码来搜索谷歌搜索中的所有顶级链接。但它一无所获 import webbrowser, requests from bs4 import BeautifulSoup string = 'selena+gomez' website = f'http://google.com/search?q={string}' req_web = requests.get(website).text parser = BeautifulSoup(req_web, 'html.parser') gotol

我做了这个代码来搜索谷歌搜索中的所有顶级链接。但它一无所获

import webbrowser, requests
from bs4 import BeautifulSoup
string = 'selena+gomez'
website = f'http://google.com/search?q={string}'
req_web = requests.get(website).text
parser = BeautifulSoup(req_web, 'html.parser')
gotolink = parser.find('div', class_='r').a["href"]
print(gotolink)

谷歌需要您指定
User-Agent
http头才能返回正确的页面。如果没有指定正确的
用户代理
,Google将返回不包含
标签且带有
r
类的页面。当您使用和不使用
用户代理进行
打印(汤)
时,都可以看到它

例如:

import requests
from bs4 import BeautifulSoup

string = 'selena+gomez'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0'}
website = f'http://google.com/search?hl=en&q={string}'

req_web = requests.get(website, headers=headers).text
parser = BeautifulSoup(req_web, 'html.parser')
gotolink = parser.find('div', class_='r').a["href"]
print(gotolink)
印刷品:

https://www.instagram.com/selenagomez/?hl=en

这(对我)很有帮助。“你能补充一句话说明这是如何解决问题的吗?”安德烈·詹姆斯补充道。归根结底,如果没有
用户代理
Goggle,它将返回您在浏览器中看到的其他版本的HTML。@AndrejKesely非常感谢兄弟!!!!这解决了我的问题。。。