python - 无法找到谷歌搜索结果的文本
问题描述
我一直在尝试使用 BeautifulSoup 在google上查找每个搜索结果的文本。使用开发人员工具,我可以看到这是由<h3>
带有class
“LC20lb DKV0Md”的 a 表示的。
但是我似乎无法使用 BeautifulSoup 找到它。我究竟做错了什么?
import requests
from bs4 import BeautifulSoup
res = requests.get('http://google.com/search?q=world+news')
soup = BeautifulSoup(res.content, 'html.parser')
soup.find_all('h3', class_= 'LC201b DKV0Md')
解决方案
您不必按 搜索class
,只需包含 a和 than的select
所有内容即可:<h3>
<div>
get_text()
import requests
from bs4 import BeautifulSoup
res = requests.get('http://google.com/search?q=world+news')
soup = BeautifulSoup(res.content, 'html.parser')
[x.get_text() for x in soup.select('h3 div')]
输出:
['World - BBC News',
'BBC News World',
'Latest news from around the world | The Guardian',
'World - breaking news, videos and headlines - CNN',
'CNN International - Breaking News, US News, World News and Video',
'Welt-Nachrichten',
'BBC World News (Fernsehsender)',
'World News - Breaking international news and headlines | Sky News',
'International News | Latest World News, Videos & Photos -ABC',
'World News Headlines | Reuters',
'World News - Hindustan Times',
'World News | International Headlines - Breaking World - Global News']
推荐阅读
- roomle - 需要 Roomle Blender 插件来准备模型以作为外部网格加载
- spring - 如何在 Spring Web 服务 PayloadValidatingInterceptor 中访问请求标头
- sql - 当记录不存在时忽略 WHERE 子句
- azure-devops - Azure DevOps - dotnet 和 js 的测试覆盖率
- pandas - 运行 python3 main.py 文件时出错
- asp.net-core-webapi - 输入不包含任何 JSON 令牌。期望输入以 LIVE Web API 上的有效 JSON 令牌开始,但在本地工作正常
- google-apps-script - 导出PDF时有没有办法强制分页?
- excel - 需要设置工作表以便代码工作,无论我在哪个工作表上
- javascript - Angular - 表单内的组件 - formControlName 必须与父级一起使用
- mongodb - GitHub 操作无法连接到 MongoDB 服务