简体   繁体   English

无法使用 BeautifulSoup 从 web 页面获取更新的数据

[英]Not able to get updated data from web page using BeautifulSoup

import requests 
URL = 'https://www.moneycontrol.com/india/stockpricequote/cigarettes/itc/ITC'
response = requests.get(URL)  
soup = BeautifulSoup(response.text,'html.parser')
#     time.sleep(5)
var1 = float(soup.find('td', attrs={'class': 'espopn'}).get_text().replace(",",""))

With this code, I am able to the value of var1, but the web page which I am accessing not showing real-time data once we land on the web page, it took 1 sec to update the real-time value once we land on the web page.使用此代码,我可以获取 var1 的值,但是我访问的 web 页面在我们登陆 web 页面后不显示实时数据,一旦我们登陆,更新实时值需要 1 秒web 页面。

Due to which the value that I am getting in var1 is not a real-time value.因此,我在 var1 中获得的值不是实时值。 Wanted to know how I can wait once I land on the web page before doing web scraping .想知道在进行 web抓取之前登陆 web 页面后如何等待。 Thanks in Advance.提前致谢。

1.As Data is updating dynamic so hard to get from bs4 so you can try from api itself so how to find it 1.由于数据正在更新动态,因此很难从bs4获取,因此您可以从 api 本身尝试如何找到它

2.Go to chrome developer mode and then Network tab find xhr and now reload your website under Name tab you will find links but there are lot of 2.Go 到 chrome 开发者模式,然后网络选项卡找到 xhr,现在在名称选项卡下重新加载您的网站,您会找到链接,但有很多

3.But on left side there is search so you can search price and from it gives url and you click on that go to headers copy that url and make call using requests module 3.但是在左侧有搜索,因此您可以搜索价格并从中给出 url 并单击 go 到标题复制该 Z572D4E421E5E6B9BC11D815E8A027112 并使用requests

import requests
from bs4 import BeautifulSoup

res=requests.get("https://api.moneycontrol.com/mcapi/v1/stock/get-stock-price?scIdList=ITC%2CVST%2CGPI%2CIWP540954%2CGTC&scId=ITC")
main_data=res.json()
main_data['data'][0]

Output: Output:

{'companyName': 'ITC',
 'lastPrice': '215.25',
 'perChange': '-0.62',
 'marketCap': '264947.87',
 'scTtm': '19.99',
 'perform1yr': '7.33',
 'priceBook': '4.16'}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM