【问题标题】:Python 'requests' with proxy not working / leaking IP?代理不工作/泄漏IP的Python“请求”?
【发布时间】:2014-09-13 22:09:54
【问题描述】:

(强烈)相关于:Requests Proxy not Working 我是 python 新手,所以为我可能犯的错误道歉。

无论我选择什么代理,每当我在http://www.whatismyproxy.com/ 上检查我的 IP 时,它都会显示我的真实 IP 和“可能存在代理”的注释。我已经尝试了数十种来自不同来源的不同代理......总是被宣布为“精英”。显然,问题出在我的代码上。这里什么都没有:

from lxml import html
import requests
base_url = 'http://www.whatismyproxy.com/'

def pagefetch(url):
    httpproxy = "http://111.13.109.51"
    proxydict = {
                "http_proxy": httpproxy
                }

    page = requests.get(url, proxies=proxydict)
    return page

def scrape1(base_url):
    page = pagefetch(base_url)
    tree = html.fromstring(page.text)
    head1 = tree.xpath('//p[@class="h1"]/text()')
    return head1

txt1 = scrape1(base_url)
print txt1

这是我目前正在开发的爬虫的简化版本,因此有点笨拙。 澄清一下,我连接到代理没有问题。 在此先感谢 =) 我正在使用 ubuntu 14.04 顺便说一句。

【问题讨论】:

    标签: python-2.7 proxy web-scraping lxml python-requests


    【解决方案1】:

    您的proxydict 错误。应该是

    proxydict = {
        'http': httpproxy
    }
    

    如果您想将代理指定为脚本的参数,您可以这样做:

    http_proxy='http://111.13.109.51' python my_script.py
    

    【讨论】:

    • ...哦,哇。那只是……尴尬。谁他妈让我这样设置的!?该死的互联网!十分感谢! =) 不用说,这对我有用。
    • 很容易混淆他们。我至少做过几次,我是请求的核心开发人员=P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2021-03-07
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多