|
|
发表于 2017-11-23 09:10:17
|
显示全部楼层
仔细瞧瞧吧
- >>> import urllib.request
- >>>
- >>> url = 'http://jandan.net/ooxx/'
- >>> req = urllib.request.Request(url)
- >>> req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36')
- >>>
- >>> response=urllib.request.urlopen(url)
- Traceback (most recent call last):
- File "<pyshell#8>", line 1, in <module>
- response=urllib.request.urlopen(url)
- File "D:\Program Files\Python36\lib\urllib\request.py", line 223, in urlopen
- return opener.open(url, data, timeout)
- File "D:\Program Files\Python36\lib\urllib\request.py", line 532, in open
- response = meth(req, response)
- File "D:\Program Files\Python36\lib\urllib\request.py", line 642, in http_response
- 'http', request, response, code, msg, hdrs)
- File "D:\Program Files\Python36\lib\urllib\request.py", line 570, in error
- return self._call_chain(*args)
- File "D:\Program Files\Python36\lib\urllib\request.py", line 504, in _call_chain
- result = func(*args)
- File "D:\Program Files\Python36\lib\urllib\request.py", line 650, in http_error_default
- raise HTTPError(req.full_url, code, msg, hdrs, fp)
- urllib.error.HTTPError: HTTP Error 403: Forbidden
- >>>
- >>> response=urllib.request.urlopen(req)
- >>>
- >>> response.read()[:32]
- b'<!DOCTYPE html>\r\n<html dir="ltr"'
- >>>
复制代码 |
|