无法获得python pandas数据框中的Web链接的输出

问题描述 投票:0回答:1

我正在尝试使用请求从指向熊猫数据框的链接中获取数据,但无法获得相同的数据。需要帮助:

import pandas as pd
import requests

url = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=ZEEL&series=EQ&fromDate=undefined&toDate=undefined&datePeriod=3months"

data = requests.get(url)

print(data)
python pandas
1个回答
0
投票

首先,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()
© www.soinside.com 2019 - 2024. All rights reserved.