gpt4 book ai didi

python - 如何从Google搜索信息栏中获取文本数据

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:17 25 4
gpt4 key购买 nike

我需要从谷歌搜索引擎信息栏中获取文本数据。如果有人使用关键字“siemens”在google搜索引擎上进行搜索。谷歌搜索结果的右侧会出现一个小信息栏。我想收集该信息栏的一些文本信息。我怎样才能使用 requests 和 Beautifulsoup 来做到这一点?这是我写的代码的一些内容。

from bs4 import BeautifulSoup as BS
import requests
from googlesearch import search
from googleapiclient.discovery import build

url = 'https://www.google.com/search?ei=j-iKXNDxDMPdwALdwofACg&q='


com = 'siemens'

#for url in search(com, tld='de', lang='de', stop=10):
# print(url)

response = requests.get(url+com)
soup = BS(response.content, 'html.parser')

红色标记区域为信息栏

enter image description here

最佳答案

您可以使用 BeautifuLSoup 中的查找功能来检索具有给定类名、id、css 选择器、xpath 等的所有元素。如果您检查信息栏(右键单击它并给出“检查”),您可以找到该栏的唯一类名或 ID。使用它可以从 BeautifulSoup 解析的整个 html 中单独过滤信息栏。

查看 BeautifulSoup 中的 find() 和 findall() 来实现您的输出。始终首先通过 id 查找,因为每个 id 对于 html 元素来说都是唯一的。如果没有相应的 id,则选择其他选项。

要获取网址,请使用 google.com/search?q=[],并在 [] 内添加搜索查询。对于包含多个单词的查询,请在其间使用“+”

关于python - 如何从Google搜索信息栏中获取文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55173768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com