user9026
user9026

Reputation: 970

Getting TypeError while downloading file from url using requests module

following is my code to download a zip file from NSE (National Stock Exchange, Mumbai, India) website.

import requests

url = r"https://www1.nseindia.com/content/historical/EQUITIES/2021/NOV/cm24NOV2021bhav.csv.zip"

resp = requests.get(url)

with open("bhavcopy.zip", "wb") as f:
    f.write(resp)

I am getting the following error.

TypeError: a bytes-like object is required, not 'Response'

I am on Windows 10 OS and I am running this program from Anaconda IDE. How can I correct this ?

Thanks

Upvotes: 1

Views: 154

Answers (1)

Thobani Madonsela
Thobani Madonsela

Reputation: 66

What you want is resp.content like here:

import requests

url = r"https://www1.nseindia.com/content/historical/EQUITIES/2021/NOV/cm24NOV2021bhav.csv.zip"

resp = requests.get(url)

with open("bhavcopy.zip", "wb") as f:
    f.write(resp.content)

Upvotes: 2

Related Questions