python爬虫基础之request设置问题
来源:互联网 发布:武汉工商学院淘宝地址 编辑:程序博客网 时间:2024/05/23 13:07
代码:
# -*- 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
0 0
- python爬虫基础之request设置问题
- Python爬虫入门之request函数定制
- python爬虫之Request,BeautifulSoup进阶
- Python爬虫之超时设置
- python扩展之爬虫基础
- Python爬虫之基础篇
- 爬虫基础1--Request
- 爬虫基础1.2--Request
- python网络爬虫基础(1)--request库
- python爬虫-->爬虫基础
- python爬虫之urllib.request和cookie登录CSDN
- python基础入门之简单爬虫编写
- Python--网络爬虫之基础篇
- Python基础爬虫之百度翻译
- python爬虫之正则表达式基础
- 零基础写python爬虫之爬虫编写全记录
- 零基础写python爬虫之爬虫编写全记录
- Python爬虫入门二之爬虫基础了解
- 解决ScrollView滑动过程中ScrollTo()方法不起作用的问题
- spring容器归纳(四)重要
- 存储过程语法及实例
- ubuntu openvpn服务器安装
- 30. Substring with Concatenation of All Words
- python爬虫基础之request设置问题
- Java环境变量的设置
- grails常用快捷键
- JSONObject与JSONArray以及前后台数据通过json传输
- PHP中数组排序,sort,asrot,ksort的使用示例
- 简单的登录页面设计;
- OCR开发包--你的误解有多深
- input type=file 上传图片前预览 亲测支持 ie8 chrome ff android及ios浏览器(将网上的版本整合一下)
- 使用hexo搭建个人博客