python - 如何使用 bs4 打印第一个 google 搜索结果链接?

标签 python python-3.x beautifulsoup

我是Python的初学者,我正在尝试使用beautifulsoup从google获取第一个搜索结果链接,该链接存储在class='yuRUbf'的div中。当我运行脚本时,输出为“无”,这里的错误是什么。

import requests
import bs4

url = 'https://www.google.com/search?q=site%3Astackoverflow.com+how+to+use+bs4+in+python&sxsrf=AOaemvKrCLt-Ji_EiPLjcEso3DVfBUmRbg%3A1630215433722&ei=CR0rYby7K7ue4-EP7pqIkAw&oq=site%3Astackoverflow.com+how+to+use+bs4+in+python&gs_lcp=Cgdnd3Mtd2l6EAM6BwgAEEcQsAM6BwgjELACECc6BQgAEM0CSgQIQRgAUMw2WPh_YLiFAWgBcAJ4AIABkAKIAd8lkgEHMC4xMC4xM5gBAKABAcgBCMABAQ&sclient=gws-wiz&ved=0ahUKEwj849XewdXyAhU7zzgGHW4NAsIQ4dUDCA8&uact=5'

request_result=requests.get( url )
soup = bs4.BeautifulSoup(request_result.text,"html.parser")
productDivs = soup.find("div", {"class": "yuRUbf"})
print(productDivs)

最佳答案

让我们看看:

    from bs4 import BeautifulSoup
import requests, json

headers = {
    'User-agent':
    "useragent"
}


html = requests.get('https://www.google.com/search?q=hello', headers=headers).text
soup = BeautifulSoup(html, 'lxml')
# locating div element with a tF2Cxc class
# calling for <a> tag and then calling for 'href' attribute
link = soup.find('div', class_='tF2Cxc').a['href']
print(link)

输出:

''' https://www.youtube.com/watch?v=YQHsXMglC9A

关于python - 如何使用 bs4 打印第一个 google 搜索结果链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68970306/

相关文章:

python - os.path 在多个文件导入中使用 Pyinstaller 指向错误的位置

Python:检查异常引发的位置

python - 在 Anaconda 提示符下导入tensorflow

python - 代码在 Python 3 中运行,而不是在 Python 2 中运行

python - 更新最新 PySide 后 Pyinstaller 错误

python - 用python编写的端口扫描仪无法正常显示输出

python - Homebrew 和 pyenv 在 MacOSX Yosemite 上的共存

python - BeautifulSoup:只要进入一个标签,不管有多少封闭标签

python - 从抓取的 HTML 数据写入 CSV

Python BeautifulSoup 城市抓取并发症