Get a file from an ASPX webpage using Python

后端 未结 1 1504
夕颜
夕颜 2021-01-22 00:16

I\'m trying to download a CSV file from this site, but I keep getting an HTML file when I\'m using this piece of code (which used to work until a few weeks ago), or when I\'m us

相关标签:
1条回答
  • 2021-01-22 01:16

    Solved by using the Requests library instead of urllib2:

    import requests
    
    url = "http://www.....aspx?download=1"
    
    file_name = "Data.csv"
    u = requests.get(url)
    
    file_size = int(u.headers['content-length'])
    print "Downloading: %s Bytes: %s" % (file_name, file_size)
    
    with open(file_name, 'wb') as f:
        for chunk in u.iter_content(chunk_size=1024): 
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
                f.flush()
    f.close()
    
    0 讨论(0)
提交回复
热议问题