|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import urllib.request
url = 'http://dzs.qisuu.com/txt/天道信用卡.txt'
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
print(html)
以下是报错:
Traceback (most recent call last):
File "C:\Users\memedai\Desktop\loadnote.py", line 16, in <module>
response = urllib.request.urlopen(url)
File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 455, in open
response = self._open(req, data)
File "C:\Python34\lib\urllib\request.py", line 473, in _open
'_open', req)
File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 1215, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python34\lib\urllib\request.py", line 1189, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Python34\lib\http\client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "C:\Python34\lib\http\client.py", line 1118, in _send_request
self.putrequest(method, url, **skips)
File "C:\Python34\lib\http\client.py", line 975, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-13: ordinal not in range(128)
访问路径中有中文,中文属于non-ASCII text需要urllib.parse.quote
- import urllib.request
- import urllib.parse
- url = 'http://dzs.qisuu.com/txt/%s' % urllib.parse.quote('天道信用卡.txt')
- print(url)
- req = urllib.request.Request(url)
- req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')
- response = urllib.request.urlopen(req)
- #html = response.read().decode("gbk")
- html = response.read(1000).decode("gbk")
- print(html)
复制代码
取全部数据很慢,我截取1000
|
|