python爬虫之urllib模块和requests模块学习
2017-03-16 20:55
176 查看
今天学习了request模块和urllib模块,了解到这两个模块最大的区别就是在爬取数据的时候连接方式的不同。urllb爬取完数据是直接断开连接的,而requests爬取数据之后可以继续复用socket,并没有断开连接。
两种用法的区别,上源码:
requests:
urllib和urllib2:
总结:requests还是比urllib更简单明了的,目前还没有发现节约资源方面的具体使用,继续跟进中。
两种用法的区别,上源码:
requests:
#coding:utf-8 import requests def eazy_url_demo(url): res=requests.get(url) print '>>>>>>>Res info>>' print res.headers print 'read>>>>>>' print res.text def url_get(url): data={'param1':'hello','param2':'wrold'} res=requests.get(url,params=data) print '>>>>>>>code' print res.status_code print res.reason print '>>>>>>>Res info>>' print res.headers print 'read>>>>>>' print res.text if __name__=='__main__': # url_exp='http://httpbin.org/ip' # eazy_url_demo(url_exp) url_get1='http://httpbin.org/get' url_get(url_get1)
urllib和urllib2:
#coding:utf-8 import urllib2,urllib def eazy_url_demo(url): res=urllib2.urlopen(url) print '>>>>>>>Res info>>' print res.info() print 'read>>>>>>' print res.read() def url_get(url): data=urllib.urlencode({'param1':'hello','param2':'wrold'}) print type(url) print type(data) new_url='?'.join([url,'%s']) % data res=urllib2.urlopen(new_url) print '>>>>>>>Res info>>' print res.info() print 'read>>>>>>' print res.read() if __name__=='__main__': # url_exp='http://httpbin.org/ip' # eazy_url_demo(url_exp) url_get1='http://httpbin.org/get' url_get(url_get1)
总结:requests还是比urllib更简单明了的,目前还没有发现节约资源方面的具体使用,继续跟进中。
相关文章推荐
- python爬虫学习二: urllib2模块的学习
- python3使用urllib模块制作网络爬虫
- urllib模块爬虫在Python2在Python3中的使用
- Python基础知识——urllib模块在爬虫中的应用
- 运维学python之爬虫基础篇(三)urllib模块高级用法
- python简单爬虫(上):urllib,urllib2与cookielib三个模块
- python爬虫常用模块——urllib模块
- Python 爬虫基础 - Urllib 模块(1)
- Python中使用urllib2模块编写爬虫的简单上手示例
- python3使用urllib模块制作网络爬虫
- 学习Python爬虫(二):urllib库之parse模块、request模块
- 基于Python的urllib2模块的多线程网络爬虫程序
- python3——urllib模块的网络爬虫
- python爬虫学习(一)通过urllib2模块获取html,设置用户代理
- Python爬虫之urllib模块1
- Python中使用urllib2模块编写爬虫的简单上手示例
- python 爬虫入门(2) 爬虫基础知识 ; urllib 模块 ;urllib2 模块
- python爬虫中的 urllib 模块 浅析
- python3使用urllib模块制作网络爬虫