问题描述
我是 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)