|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import urllib.request
- url = 'http://www.whatismyip.com.tw/'
- prosy_support = urllib.request.ProxyHandler({'http':'120.133.60.92:80'})
- opener = urllib.request.build_opener(prosy_support)
- opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47')]
- urllib.request.install_opener(opener)
- response = opener.open(url)
- html = response.read().decode('utf-8')
- print(html)
复制代码
报错- Traceback (most recent call last):
- File "<input>", line 12, in <module>
- File "D:\Python\Python37\lib\urllib\request.py", line 531, in open
- response = meth(req, response)
- File "D:\Python\Python37\lib\urllib\request.py", line 641, in http_response
- 'http', request, response, code, msg, hdrs)
- File "D:\Python\Python37\lib\urllib\request.py", line 569, in error
- return self._call_chain(*args)
- File "D:\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
- result = func(*args)
- File "D:\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
- raise HTTPError(req.full_url, code, msg, hdrs, fp)
- urllib.error.HTTPError: HTTP Error 403: Forbidden
复制代码
大神们,能不能告诉下我,如何看出网站是根据什么拒绝我爬虫的?是我代码哪里没伪装好?
我用requests模块 没有问题
urllib 不喜欢 所以没有研究
你的代码 不外两点,一 不知道你添加的代理 是什么代理,你能保证该代理 是可用的吗
另 不知道你的headers 添加的是否正确
我们headers 都是headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47'}
这个样子的
|
|