【问题标题】:How to use proxy to download a zip file with url如何使用代理下载带有 url 的 zip 文件
【发布时间】:2020-11-27 11:17:39
【问题描述】:

我已经实现了一个代码来下载股票市场中所有日期的 bhav 副本。刮了大约 2 年,我的 IP 好像被封了。

此代码对我不起作用。

import urllib.request
url = 'https://www1.nseindia.com/content/historical/DERIVATIVES/2014/APR/fo01APR2014bhav.csv.zip'
response = urllib.request.urlopen(url)

它给出了以下错误:

urllib.error.HTTPError: HTTP Error 403: Forbidden

我想知道如何使用代理来获取数据。任何帮助将不胜感激。

【问题讨论】:

    标签: python web-scraping python-requests urllib


    【解决方案1】:
    import urllib.request
    
    proxy_host = '1.2.3.4:8080'    # host and port of your proxy
    url = 'https://www1.nseindia.com/content/historical/DERIVATIVES/2014/APR/fo01APR2014bhav.csv.zip'
    
    req = urllib.request.Request(url)
    req.set_proxy(proxy_host, 'http')
    
    response = urllib.request.urlopen(req)
    

    为了获得更大的灵活性,您可以使用代理处理程序 - https://docs.python.org/3/library/urllib.request.html

    proxy_handler = urllib.request.ProxyHandler({'http': '1.2.3.4:3128/'})
    proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
    proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
    
    opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
    

    【讨论】:

    • response = urllib.request.urlopen(req) 行似乎一直在等待,它根本没有完成执行。
    • @RamHarnish 它依赖于代理服务器。代理服务器会重定向。
    • @RamHarnish 我认为从代理服务器获得响应需要更多时间
    • 它没有解决我的问题,它给出了这个错误:URLError: <urlopen error [Errno 60] Operation timed out>
    • 表示连接代理服务器超时
    【解决方案2】:

    这很好用,

    import requests
    
    headers = {
        'authority': 'www.nseindia.com',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
        'accept': '*/*',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://www.nseindia.com/content/',
        'accept-language': 'en-US,en;q=0.9,lb;q=0.8',
    
    }
    
    url = "https://www1.nseindia.com/content/historical/DERIVATIVES/2014/APR/fo01APR2014bhav.csv.zip"
    
    r = requests.get(url,headers=headers)
    
    with open("data.zip","wb") as f:
        f.write(r.content)
    

    如果你有代理,

    proxy = {"http" : "x.x.x.x:pppp",
             "https" :"x.x.x.x:pppp",
            }
    r = requests.get(url, headers=headers, proxies=proxy)
    

    【讨论】:

    • 谢谢,你知道我在哪里可以找到代理吗?
    • 免费代理不能很好地工作。你可以谷歌搜索付费代理,如 crawlera、geosurf 等。
    • 我会尝试一下,看看它是否有效,我之前不想接受,因为如果这个答案不起作用,其他人不会回答它。
    【解决方案3】:

    您无需使用代理来下载此文件。下面的代码就像一个魅力:

    import urllib.request
    url = 'https://www1.nseindia.com/content/historical/DERIVATIVES/2014/APR/fo01APR2014bhav.csv.zip'
    
    req = urllib.request.Request(url)
    
    # Add referer header to bypass "HTTP Error 403: Forbidden"
    req.add_header('Referer', 'https://www.nseindia.com')
    res = urllib.request.urlopen(req)
    
    # Save it into file.zip
    with open("file.zip", "wb") as f:
        f.write(res.read())
    

    如果您想获得免费代理,请访问https://free-proxy-list.net/。然后在https://stackoverflow.com/a/63328368/8009647@pyd 回复@

    【讨论】:

      猜你喜欢
      • 2017-01-10
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      相关资源
      最近更新 更多