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

问题描述 投票:1回答:1

我需要从谷歌搜索引擎信息栏中获取文本数据。如果有人使用关键字“siemens”来搜索谷歌搜索引擎。谷歌搜索结果右侧显示一个小信息栏。我想收集该信息栏的一些文本信息。我怎么能使用请求和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

python beautifulsoup request
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.