python - 谷歌字典结果抓取
问题描述
我正在尝试抓取谷歌的字典结果,但我认为我做错了。这是我要抓取的内容的屏幕截图:
这是我正在运行的代码:
elif 'meaning' in query:
query = query.replace("what", "").replace("meaning", "").replace("means", "").replace("is the", "").replace("of","")
headers = {
'User-agent':
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
}
r = requests.get(f"https://www.google.com/search?q={query}", headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find('div', class_= 'LTKOO sY7ric')
print(result.span.text)
speak(result.span.text)
我收到此错误:
Traceback (most recent call last):
File "c:\MY Programmes\DUSTIN- THE VOICE ASSISTANT\dustin.py", line 106, in <module>
print(result.span.text)
AttributeError: 'NoneType' object has no attribute 'span'
解决方案
很可能,当您尝试时该页面尚未加载,soup.find(query)
因此您可以执行time.sleep(x)
或使用 wget 下载页面,并在下载后使用 soup 进行查询。
推荐阅读
- arrays - 如何在只有某些条件需要为 TRUE 的数组公式中包含 OR - Excel 第 2 部分
- vba - 关闭时访问崩溃
- jquery - 如何在单个 Swiper Slider 上添加多个分页
- r - 具有 2 个组的叠加 ggplot2 条形图
- flutter - 如何在 Flutter 中扩展孩子的全宽?
- java - @JsonView 未传播到(“嵌套”)自定义序列化程序
- docker - 在 docker 容器内进行定期清理的最佳方法是什么?
- nuget - Azure Artifacts 的 Cake nuget 插件问题
- php - wordpress php无法将字符串与代码结合起来
- node.js - 等待将未定义返回到异步函数中(var all_courses 未定义)