python - 使用 BS4 找不到 Google 搜索的结果 - Python
问题描述
我试图从谷歌搜索中取回股票价格,但 BS4 的结果超过 300 行。
这是我的代码:
import bs4, requests
exampleFile = requests.get('https://www.google.com/search?q=unip6')
exampleSoup = bs4.BeautifulSoup(exampleFile.text, features="html.parser")
elems = exampleSoup.select('div', {"class": 'IsqQVc NprOob'})
print(len(elems))
for each in elems:
print(each.getText())
print(each.attrs)
print('')
我希望结果只是价格:'23,85'
解决方案
在这种情况下,页面不是动态加载的,因此可以在汤中找到目标详细信息。通过不使用类选择器,也可以避免更改类名的问题(至少暂时......):
for s in soup.select("div"):
if 'Latest Trade' in s.text:
print(s.text.split('Latest Trade. ')[1].split('BRL')[0])
break
输出:
23.85
推荐阅读
- ios - “通话中位置的额外参数”是什么意思?
- sql - 突然出现一个简单查询的 SQL 转换错误
- python - 如何强制matlibplot在python中以正确的顺序放置xtixck
- javascript - 错误:[Immer] 一个 immer 生产者返回了一个新值*并*修改了它的草稿。返回一个新值*或*修改草稿。(棋盘游戏.io)
- sql - SET 值 SELECT FROM 变量表
- python - 函数将参数视为字符串而不是整数
- reactjs - React/Graph - 将电子邮件移动到 Outlook Web 加载项中的新文件夹
- ios - 德语 VoiceOver 读取视图上的“accessibilityLabel”字符串“Feedback”时未指定的文本“Komma”来自哪里?
- regex - 正则表达式用于识别字母数字值以外的值,这些值之间可以有连字符或点,但不能在开头或结尾
- python - 避免循环中的重复检查