python3中使用 urllib 出问题
因为我的python版本是3.6的,所以引入的是 import urllib.request
然后使用 response = urllib.request.urllopen(url)一直出问题,这是怎么回事呢。
import urllib.request import http.cookiejar url = "www.baidu.com" #--------------方法一-------------- #获取地址内容 response3 = urllib.request.urlopen(url) #判断状态码等于200成功 print (response3.getcode()) #读取获取的内容 cont = response3.read() print (cont)
问题提示:
Traceback (most recent call last): File "/Users/carol/www/Python/urllib2DownloadStyle.py", line 9, in <module> response3 = urllib.request.urlopen(url) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 511, in open req = Request(fullurl, data) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 329, in __init__ self.full_url = url File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 355, in full_url self._parse() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 384, in _parse raise ValueError("unknown url type: %r" % self.full_url) ValueError: unknown url type: 'www.baidu.com' [Finished in 0.3s]