首页 > 解决方案 > 网页从浏览器到 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)

打印的结果不包含任何报价或股票或任何东西。

我正在寻找的是这些符号.... 在此处输入图像描述

标签: python-3.xbeautifulsoup

解决方案


您可以在开发人员模式下使用 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
...

图片: 在此处输入图像描述


推荐阅读