繁体   English   中英

使用 Python 中的代理访问网页

[英]Accessing Webpages Using Proxies In Python

我是 python 的新手。 我正在做 web 刮。 我使用 googlesearch 模块获取 python 中的链接。 但是经过这么多请求,谷歌阻止了我的 ip。 所以我用了tor,然后用袜子我做了同样的任务,但同样的事情又发生了。 现在我提出了应该使用代理的解决方案。 但是当我使用代理发出请求时,它会引发异常。 下面是我使用的代码。 我通过手动设置代理来运行 chrome 浏览器,它运行良好,但是为什么当我使用 python 访问时它会抛出异常。

import requests
    proxies = {'http': 'socks5://user:pass@host:port',
               'https': 'socks5://user:pass@host:port'}
    resp = requests.get('http://https://www.google.com', proxies=proxies )

请检查您的 URL。 URL 应该是https://www.google.com/但您的代码有错误的 URL。 除此之外,如果您仍然遇到错误,请尝试在请求中添加用户代理。

import requests

proxies = {
    'http': 'socks5://user:pass@host:port',
    'https': 'socks5://user:pass@host:port'
}
headers = {
    "User-Agent": "Mozilla/5.0 (X11Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
}
resp = requests.get('https://www.google.com', proxies=proxies, headers=headers)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM