今天学习了request模块和urllib模块,了解到这两个模块最大的区别就是在爬取数据的时候连接方式的不同。urllb爬取完数据是直接断开连接的,而requests爬取数据之后可以继续复用socket,并没有断开连接。
两种用法的区别,上源码:
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更简单明了的,目前还没有发现节约资源方面的具体使用,继续跟进中。
點(diǎn)擊查看更多內(nèi)容
9人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦