使用对下载csv文件的url的请求

a9wyjsp7  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(112)

通过下面的链接下载了一个csv文件。我无法将其用作pandas Dataframe
这里的标题必须复制粘贴从红框所附的图像作为网站不支持一般的要求。
this is the NSE website .
this is the Dev Tools from where the headers has to be copied

import requests
import pandas as pd
import csv

header = ''''''


def header_in_dictionary(header):
    unwanted = '''sec-ch-ua: "Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin'''
    
    header = header.replace(unwanted,'')
    
    header_list = header.split('\n')
    
    
    key_list = ['accept','accept-encoding','accept-language','cookie','referer','user-agent','x-requested-with']
    
    value_list = []
    
    for ele in header_list:
        if ':' in ele:
            value_list.append(ele[ele.index(':') + 2:len(ele)])
        else:
            None
    

    
    header_dictionary = {}
    index_counter = 0
    for ele in key_list :
        header_dictionary[ele] = value_list[index_counter]
        index_counter += 1
    
      
    return header_dictionary


aa = requests.get("https://www.nseindia.com/api/historical/cm/equity?symbol=ASIANPAINT&series=[%22EQ%22]&from=01-04-2021&to=9-03-2023&csv=true", headers = header_in_dictionary(header))

我想使用csv文件的数据。为此,我必须将其转换为pandas Dataframe 。

6uxekuva

6uxekuva1#

使用requests.Session,因为您需要存储cookie:

import pandas as pd
import requests
import io

headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0'}

# Create a HTTP session
session = requests.Session()

# Get cookies
response = session.get('https://www.nseindia.com/', headers=headers)

# Download csv
response = session.get("https://www.nseindia.com/api/historical/cm/equity?symbol=ASIANPAINT&series=[%22EQ%22]&from=01-04-2021&to=9-03-2023&csv=true", headers=headers)

# Create dataframe
df = pd.read_csv(io.BytesIO(response.content))

输出:

>>> df
           Date  series      OPEN      HIGH       LOW  PREV. CLOSE       ltp     close      vwap     52W H     52W L   VOLUME             VALUE   No of trades 
0    09-Mar-2023      EQ  2,862.00  2,872.45  2,836.85     2,859.55  2,856.00  2,853.55  2,855.48  3,582.90  2,560.00   774224  2,210,782,913.25          51928
1    08-Mar-2023      EQ  2,870.00  2,870.00  2,827.00     2,864.55  2,857.00  2,859.55  2,850.88  3,582.90  2,560.00   580250  1,654,225,600.65          50105
2    06-Mar-2023      EQ  2,843.00  2,884.00  2,830.25     2,828.85  2,861.60  2,864.55  2,869.75  3,582.90  2,560.00  1050267  3,013,998,661.15          74159
3    03-Mar-2023      EQ  2,837.05  2,852.00  2,810.65     2,833.90  2,830.15  2,828.85  2,826.34  3,582.90  2,560.00   679295  1,919,921,555.35          64015
4    02-Mar-2023      EQ  2,831.00  2,849.00  2,811.00     2,840.90  2,833.00  2,833.90  2,832.79  3,582.90  2,560.00   604523  1,712,488,953.55          44537
..           ...     ...       ...       ...       ...          ...       ...       ...       ...       ...       ...      ...               ...            ...
477  08-Apr-2021      EQ  2,630.00  2,658.40  2,628.00     2,628.60  2,643.95  2,650.65  2,647.85  2,873.45  1,482.95   897483  2,376,398,265.70          60031
478  07-Apr-2021      EQ  2,605.00  2,668.05  2,600.10     2,611.60  2,621.60  2,628.60  2,644.44  2,873.45  1,482.95  1937977  5,124,871,995.00         110658
479  06-Apr-2021      EQ  2,526.00  2,620.00  2,504.50     2,510.65  2,610.10  2,611.60  2,585.83  2,873.45  1,482.95  3372846  8,721,612,490.50         178805
480  05-Apr-2021      EQ  2,558.00  2,558.00  2,484.50     2,551.75  2,512.00  2,510.65  2,509.91  2,873.45  1,482.95  1268939  3,184,919,119.35          74815
481  01-Apr-2021      EQ  2,532.00  2,564.55  2,532.00     2,537.40  2,545.00  2,551.75  2,549.80  2,873.45  1,482.95  1219588  3,109,711,122.00          54928

[482 rows x 14 columns]

相关问题