代码:
# -*- coding:utf-8 -*-
import urllib
import urllib2
page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0'
headers = {'User-Agent': user_agent}
try:
request = urllib2.Request(url, headers)
***************************问题所在
改成:
request = urllib2.Request(url, headers=headers)
************************************
response = urllib2.urlopen(request)
print response.read()
except urllib2.URLError, e:
if hasattr(e, 'code'):
print e.code
if hasattr(e, 'reason'):
print e.reason
报错:
G:\python27\python.exe D:/编程/PY/test_new.py
Traceback (most recent call last):
File "D:/���/PY/test_new.py", line 11, in <module>
response = urllib2.urlopen(request)
File "G:\python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "G:\python27\lib\urllib2.py", line 400, in open
response = self._open(req, data)
File "G:\python27\lib\urllib2.py", line 418, in _open
'_open', req)
File "G:\python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "G:\python27\lib\urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "G:\python27\lib\urllib2.py", line 1174, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "G:\python27\lib\httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "G:\python27\lib\httplib.py", line 992, in _send_request
self.endheaders(body)
File "G:\python27\lib\httplib.py", line 954, in endheaders
self._send_output(message_body)
File "G:\python27\lib\httplib.py", line 818, in _send_output
self.send(message_body)
File "G:\python27\lib\httplib.py", line 790, in send
self.sock.sendall(data)
File "G:\python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not dict
Process finished with exit code 1
# -*- coding:utf-8 -*-
import urllib
import urllib2
page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0'
headers = {'User-Agent': user_agent}
try:
request = urllib2.Request(url, headers)
***************************问题所在
改成:
request = urllib2.Request(url, headers=headers)
************************************
response = urllib2.urlopen(request)
print response.read()
except urllib2.URLError, e:
if hasattr(e, 'code'):
print e.code
if hasattr(e, 'reason'):
print e.reason
报错:
G:\python27\python.exe D:/编程/PY/test_new.py
Traceback (most recent call last):
File "D:/���/PY/test_new.py", line 11, in <module>
response = urllib2.urlopen(request)
File "G:\python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "G:\python27\lib\urllib2.py", line 400, in open
response = self._open(req, data)
File "G:\python27\lib\urllib2.py", line 418, in _open
'_open', req)
File "G:\python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "G:\python27\lib\urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "G:\python27\lib\urllib2.py", line 1174, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "G:\python27\lib\httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "G:\python27\lib\httplib.py", line 992, in _send_request
self.endheaders(body)
File "G:\python27\lib\httplib.py", line 954, in endheaders
self._send_output(message_body)
File "G:\python27\lib\httplib.py", line 818, in _send_output
self.send(message_body)
File "G:\python27\lib\httplib.py", line 790, in send
self.sock.sendall(data)
File "G:\python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not dict
Process finished with exit code 1