如何解决在 Python 中使用代理访问网页
我是 Python 新手。我正在做网页抓取。我使用 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 应该是 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。