如何使用python从网站实时获取一些数据?

2024-10-04 11:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从网站获取som数据

https://web.sensibull.com/optionchain?expiry=2020-03-26&tradingsymbol=NIFTY

The ATMIV data as in the picture 我正在使用beautifulsoup库获取此数据,并尝试了以下代码:

import requests

import urllib.request

import time

from bs4 import BeautifulSoup

url = 'https://web.sensibull.com/optionchain?expiry=2020-03-26&tradingsymbol=NIFTY'

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')

b = soup.find("div", {"class": "style__AtmIVWrapper-idZNMX kUMMRI"})

print(b)

但它显示“无”作为输出

虽然在完整的HTML代码中只有一个同名类,但我也尝试了以下方法:

for b in soup.find_all('div', attrs={'class':'style__AtmIVWrapper-idZNMX kUMMRI'}):

    print(b.get_text())

    print(len(b))

但它不起作用

还尝试了汤。查找(“div”) 但它没有在输出中显示所需的div标记,可能是因为存在嵌套的div

无法获取此数据并继续我的工作。请帮忙


Tags: 数据代码httpsimportdivcomwebrequests
2条回答

可能是语法问题,请尝试使用soup.find_all("div", class_="style__AtmIVWrapper-idZNMX kUMMRI")或仅使用soup.find("div", class_="style__AtmIVWrapper-idZNMX kUMMRI")

如果对webscraping和bs4感兴趣,请查看文档https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find

如果您正在寻找代码。这可能有助于:

from selenium import webdriver 
import time
webpage = 'https://web.sensibull.com/optionchain?expiry=2020-03-26&tradingsymbol=NIFTY'
driver = webdriver.Chrome(executable_path='Your/path/to/chromedriver.exe') 
driver.get(webpage)
time.sleep(10)
nifty_fut = driver.find_element_by_xpath('//*[@id="app"]/div/div[4]/div[2]/div[3]/div/div/div[2]/div[1]/div[1]/div/button/span[1]/div[1]')
print(nifty_fut.text)
atm_iv = driver.find_element_by_xpath('//*[@id="app"]/div/div[4]/div[2]/div[3]/div/div/div[2]/div[1]/div[2]')
print(atm_iv.text)
driver.quit()

相关问题 更多 >