簡體   English   中英

如何使用python從網站實時獲取一些數據?

[英]How to get some data in real time from a website using python?

我想從網站獲取一些數據

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

ATMIV數據如圖 我正在使用 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))

但它不起作用。

也試過soup.find("div") 但它沒有在輸出中顯示所需的div 標簽,可能是由於存在嵌套的div。

無法獲取此數據並繼續我的工作。 請幫忙。

可能是一個語法問題,嘗試使用soup.find_all("div", class_="style__AtmIVWrapper-idZNMX kUMMRI")或者只是soup.find("div", class_="style__AtmIVWrapper-idZNMX kUMMRI")

如果對網頁抓取和 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM