【问题标题】:unable to get the output of a web link in a python pandas dataframe无法在 python pandas 数据框中获取 Web 链接的输出
【发布时间】:2020-02-24 03:25:23
【问题描述】:

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

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】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-22
      • 2015-05-25
      • 2021-07-22
      • 2016-10-03
      • 2020-12-05
      • 2015-05-12
      • 1970-01-01
      • 2020-11-01
      相关资源
      最近更新 更多