python - 无法使用 bs4 从 BSE 网站上抓取特定信息

标签 python web-scraping beautifulsoup

我正在尝试从 this 中抓取之前的收盘价和开盘价网站。这是一张图像,作为要抓取的信息所在位置的引用。

stock info table

看起来特定的表是带有 class="col-lg-13"div 标记的子级,但 bs4 仅返回 None 进行所有寻找它的尝试。

我尝试过以下方法:

from bs4 import BeautifulSoup
import requests

link = "https://bseindia.com/stock-share-price/bharat-gears-ltd/bharatgear/505688/"
resp = requests.get(link).content
soup = BeautifulSoup(resp, "lxml")

box = soup.find('div', class_="col-lg-13")
table = box.find('table')
print(table)

>>> None

我也尝试过:

container = soup.find('div', attr={'ng-init': "fnStockTrading()"})
tables = container.find_all('table')
print(tables)

>>> []

最佳答案

使用页面用于数据的相同 URL(API)。这可以在网络选项卡中找到

import requests
r = requests.get('https://api.bseindia.com/BseIndiaAPI/api/getScripHeaderData/w?Debtflag=&scripcode=505688&seriesid=').json()
prev_close = r['Header']['PrevClose']
prev_open = r['Header']['Open']
print(prev_close, prev_open)

关于python - 无法使用 bs4 从 BSE 网站上抓取特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56036758/

相关文章:

python - 如何在 Python 中解析 SRV 记录?

python - 应该是简单的 XPATH 吗?

Python BeautifulSoup 和请求网络抓取

Python 对象作用域问题

python - 了解显式添加 () 以获得评估的必要性

php - 使用PHP进行网站爬取

python - 处理类型错误 : 'NoneType' in BeautifulSoup

PYTHON - BEAUTIFULSOUP 如何将空 TD(表数据)刮取为空值而不是跳过它

python - 无法通过Python创建SO问题的MySQL数据库

selenium - 在scrapy中推迟部分抓取