首先 request.get 返回一个响应对象。您需要使用库 BeautifulSoup 或 lxml 解析响应。
另外,您需要附加一个有效的标头,否则服务器会终止请求,请参阅以下链接-
adding header to python requests module
How to use Python requests to fake a browser visit?
如果您只是想以某种方式获取数据,您可以使用 selenium 运行它(以下代码有效),尽管
这可以根据要求处理。
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
webpage = 'https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=ZEEL&series=EQ&fromDate=undefined&toDate=undefined&datePeriod=3months'
driver = webdriver.Chrome(executable_path='Your/path/to/chromedriver.exe')
driver.get(webpage)
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
table = soup.find('table')
table_rows = table.find_all('tr')
res = []
for tr in table_rows:
td = tr.find_all('td')
row = [tr.text.strip() for tr in td if tr.text.strip()]
if row:
res.append(row)
df = pd.DataFrame(res, columns=["Date", "Symbol", "Series", "Open Price","High Price","Low Price","Last Traded Price ","Close Price","Total Traded Quantity","Turnover (in Lakhs)"])
print(df)
driver.quit()