|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 y116114 于 2020-10-13 23:41 编辑
- import urllib.request
- response = urllib.request.urlopen("https://www.youzack.com")
- html = response.read()
- print(html)
复制代码- Traceback (most recent call last):
- File "E:\Python程序例子\111.py", line 2, in <module>
- response = urllib.request.urlopen("https://www.youzack.com")
- File "E:\Python\lib\urllib\request.py", line 222, in urlopen
- return opener.open(url, data, timeout)
- File "E:\Python\lib\urllib\request.py", line 531, in open
- response = meth(req, response)
- File "E:\Python\lib\urllib\request.py", line 640, in http_response
- response = self.parent.error(
- File "E:\Python\lib\urllib\request.py", line 569, in error
- return self._call_chain(*args)
- File "E:\Python\lib\urllib\request.py", line 502, in _call_chain
- result = func(*args)
- File "E:\Python\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
复制代码 [/code]
为啥按照爬虫第一课视频里讲的代码不能爬取这个网站信息啊?一脸懵逼。。。。
这样就可以了。。。。。。。
这个库我没用过,网上找的代码,一般现在都用resquests库吧,比较方便
- import urllib.request
- url="https://www.youzack.com"
- headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
- req=urllib.request.Request(url=url,headers=headers)
- response=urllib.request.urlopen(req)
- html = response.read().decode("utf-8")
- print(html)
复制代码
|
|