python-3.x - 网页从浏览器到 API 的显示方式不同
问题描述
我正在尝试从CNBC 美国市场推动者页面中提取报价数据。
使用 BeautifulSoup4,我尝试了这个简单的页面查看。
from bs4 import BeautifulSoup
import requests
website = requests.get('https://www.cnbc.com/us-market-movers/')
soup = BeautifulSoup(website.text, 'lxml')
print(website.text)
打印的结果不包含任何报价或股票或任何东西。
解决方案
您可以在开发人员模式下使用 chrome 找到此数据,然后转到网络配置文件并刷新您的网站
然后转到 xhr 标签通过不同的链接查找您的特定数据,从图像中您可以看到我找到了 json 数据,现在您可以复制链接地址并使用 json 数据
import requests
res=requests.get("https://gdsapi.cnbc.com/market-mover/groupMover/SP500/CHANGE_PCT/BOTH/12.json?source=SAVED&delayed=false&partnerId=2")
data=res.json()
for d in data['rankedSymbolList']:
for main_data in d['rankedSymbols']:
print(main_data['symbolDesc'])
输出:
Seagate Technology PLC
Western Digital Corp
Occidental Petroleum Corp
Baker Hughes Co
Newmont Corporation
...
推荐阅读
- excel - Excel VBA 函数仅返回数组中值或文本的地址返回 #VALUE
- java - 方法中的数组值不能返回给main(java)
- sql - 基于两个唯一标识符的工作时间总和
- javascript - 如何重置 AWS Cognito 用户的密码?
- netlogo - 将相同的项目添加到更多列表
- r - lme4 输出中的未知符号
- apostrophe - 为什么撇号不断发出 POST 调用来检查用户是否登录?
- typescript - 循环遍历泛型中的属性
- base64 - 将 Base64 图像从横向转换为纵向
- java - gradle - plugins {} (脚本块) vs pluginManager.apply()