python 中网络编程连接以及报头
2016-12-20 12:51
295 查看
urllib:
url ="http://www.csdn.net"
ff = urllib.urlopen(url)(url须为绝对地址)
不能修改header
ff.geturl() 获取访问的URL(string)
ff.headers 获取回应报头(string)
ff.info() 获取回应报头(string)
urllib2:
ff = urllib2.urlopen(url)(同urllib,(url须为绝对地址))
另一种方式(可修改header,有时网站会屏蔽不是浏览器的爬虫请求,需要修改header,加入user-agent等):
reqheaders={'':'','':''}
request = urllib2.Request(url,headers=reqheaders)
respon = urllib2.urlopen(request)
respon.info()/respon.headers()/info()(类似urllib)
httplib:
conn = httplib.HTTPConnection(url)(url可不必为绝对地址)重定向会停止
conn = httplib.HTTPSConnection(url)(url可不必为绝对地址)
conn.request('GET',
'/')
res = conn.getresponse()获取网站回应
header = res.getheaders()获取报头(list)
res.getheaders(name,)字典查询特定name的报头
httplib2:
http = httplib2.Http()
#http.request(uri, method, body, headers, redirections, connection_type)
response, content = http.request(url2)
response 为headers字典,content为内容string
可以不区分HTTPS或者http,会自动重定向,追踪到最后的页面。
url ="http://www.csdn.net"
ff = urllib.urlopen(url)(url须为绝对地址)
不能修改header
ff.geturl() 获取访问的URL(string)
ff.headers 获取回应报头(string)
ff.info() 获取回应报头(string)
urllib2:
ff = urllib2.urlopen(url)(同urllib,(url须为绝对地址))
另一种方式(可修改header,有时网站会屏蔽不是浏览器的爬虫请求,需要修改header,加入user-agent等):
reqheaders={'':'','':''}
request = urllib2.Request(url,headers=reqheaders)
respon = urllib2.urlopen(request)
respon.info()/respon.headers()/info()(类似urllib)
httplib:
conn = httplib.HTTPConnection(url)(url可不必为绝对地址)重定向会停止
conn = httplib.HTTPSConnection(url)(url可不必为绝对地址)
conn.request('GET',
'/')
res = conn.getresponse()获取网站回应
header = res.getheaders()获取报头(list)
res.getheaders(name,)字典查询特定name的报头
httplib2:
http = httplib2.Http()
#http.request(uri, method, body, headers, redirections, connection_type)
response, content = http.request(url2)
response 为headers字典,content为内容string
可以不区分HTTPS或者http,会自动重定向,追踪到最后的页面。
相关文章推荐
- python网络编程中,Cisco packet tracer 中多个路由器、两个交换机以及服务器
- [Python网络编程]gevent httpclient以及网页编码
- Python网络编程:使用pexpect实现快速ssh连接
- [网络编程] TCP、UDP区别以及TCP传输原理、拥塞避免、连接建立、连接释放总结
- Java网络编程 连接测试以及异常介绍
- Python socket编程之两种网络连接方式
- [Python网络编程]gevent httpclient以及网页编码
- python socket 编程之三:长连接、短连接以及心跳(转药师Aric的文章)
- Python的网络编程(一)
- J2ME中多线程网络连接编程的分析
- 关于python下网络编程
- Java网络编程从入门到精通(14):多种多样的建立网络连接的方式
- Linux网络编程一步一步学-接受客户端连接请求
- python天天进步(6)--网络编程之数据传输TCP
- python天天进步(8)--网络编程之文件下载
- 网络编程--客户机断开连接(WinSock)
- Python的网络编程(三)
- Python的网络编程(二)
- Python网络编程例子
- python天天进步(5)--网络编程之数据传输UDP