3 回答

TA貢獻(xiàn)1856條經(jīng)驗 獲得超11個贊
請嘗試以下操作:
pip install lxml
pip install html5lib
pip install BeautifulSoup4
現(xiàn)在您不需要導(dǎo)入請求。
import pandas as pd
import html5lib
table=pd.read_html('https://www.nseindia.com/live_market/dynaContent/live_watch/equities_stock_watch.htm')
此外,如果您打算從國家證券交易所抓取股票數(shù)據(jù),您可以使用 NSEpy,這是一個簡單的 API 來獲取印度公司的股票數(shù)據(jù)。

TA貢獻(xiàn)1864條經(jīng)驗 獲得超2個贊
您收到 AttributeError 因為 pd.read_html() 返回數(shù)據(jù)框列表,而列表沒有屬性“to_html”
來到解決方案,您提到的頁面是使用javascript呈現(xiàn)的。BeautifulSoup無法從 javascript 呈現(xiàn)的頁面中抓取數(shù)據(jù)。
要訪問 Javascript 渲染的頁面,您需要使用成熟的渲染引擎。您可以使用selenium或phantomJS來獲取 javascript 數(shù)據(jù)。

TA貢獻(xiàn)1951條經(jīng)驗 獲得超3個贊
嘗試以下...
# !pip install webdriver-manager
import numpy as np
import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
DRIVER_PATH = '/path/to/chromedriver'
url= 'https://www1.nseindia.com/live_market/dynaContent/live_watch/equities_stock_watch.htm'
options = Options()
options.headless = False
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.set_page_load_timeout(5)
try:
driver.get(url)
except:
pass
src= driver.page_source
driver.quit()
soup= bs(src, 'lxml')
table= soup.find_all('table')
table= pd.read_html(str(table[1]),header=0)[0].set_index('Symbol')
table
添加回答
舉報