【问题标题】:GET api request is working in POSTMAN but not in python code , I want to download the response as csvGET api 请求在 POSTMAN 中有效,但在 python 代码中无效,我想将响应下载为 csv
【发布时间】:2020-11-11 06:46:19
【问题描述】:

https://www.nseindia.com/api/corporates-pit?index=equities&from_date=21-04-2020&to_date=21-07-2020&csv=true

上面的url是get request Url,下面是我的python代码

import requests

url = "https://www.nseindia.com/api/corporates-pit?index=equities&from_date=21-04-2020&to_date=21-07-2020&csv=true"

payload = {}
headers= {}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

我也试过http.client

import http.client
import mimetypes
conn = http.client.HTTPSConnection("www.nseindia.com")
payload = ''
headers = {}
conn.request("GET", "/api/corporates-pit?index=equities&from_date=21-04-2020&to_date=21-07-2020&csv=true", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

错误/秒

urllib3.exceptions.ProtocolError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))
During handling of the above exception, another exception occurred:

raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None))

【问题讨论】:

    标签: python python-3.x csv python-requests postman


    【解决方案1】:

    请在下面找到您的代码的工作版本。您需要将agentcookies 变量传递给requests

    import requests
    
    csv_url = "https://www.nseindia.com/api/corporates-pit?index=equities&from_date=21-04-2020&to_date=21-07-2020&csv=true"
    
    agent = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}
    cookies = {"cookie":"COPY_HERE_YOUR_COOKIE_FROM_BROWSER"}
    
    req = requests.get(csv_url,headers=agent, cookies=cookies)
    url_content = req.content
    
    csv_file = open('downloaded.csv', 'wb')
    csv_file.write(url_content)
    csv_file.close()
    

    【讨论】:

      【解决方案2】:

      更新代码

      import requests
      
      session = requests.Session()
      print(session.cookies.get_dict())
      
      response = session.get('http://google.com')
      print(session.cookies.get_dict())
      
      from_date = '21-04-2020'
      to_date = '21-07-2020'
      
      csv_url = "https://www.nseindia.com/api/corporates-pit?index=equities&from_date="+from_date+"&to_date="+to_date+"&csv=true"
      
      agent = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}
      #cookies = {"cookie":"COPY_HERE_YOUR_COOKIE_FROM_BROWSER"}
      
      req = session.get(csv_url,headers=agent)
      url_content = req.content
      
      csv_file = open('downloaded.csv', 'wb')
      csv_file.write(url_content)
      csv_file.close()
      
      print("Done")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-11
        • 2018-07-24
        • 2019-07-19
        • 2016-07-11
        • 2018-07-14
        • 2019-11-28
        • 2017-05-03
        相关资源
        最近更新 更多