为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

翻阅古今 2023-10-10 14:48:56
我想从网站获取一些数据https://web.sensibull.com/optionchain?expiry=2020-03-26&tradingsymbol=NIFTY我正在使用 beautifulsoup 库来获取这些数据,并尝试了以下代码:import requestsimport urllib.requestimport timefrom bs4 import BeautifulSoupurl = '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))但这不起作用。还尝试了 soup.find("div") 但它没有在输出中显示所需的 div 标签,可能是由于存在嵌套的 div。无法获取此数据并继续我的工作。请帮忙。
查看完整描述

2 回答

?
米脂

TA贡献1836条经验 获得超3个赞

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


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()


查看完整回答
反对 回复 2023-10-10
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

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


查看完整回答
反对 回复 2023-10-10
  • 2 回答
  • 0 关注
  • 98 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号