tenliu的爬蟲(chóng)-python的urllib庫(kù)

學(xué)習(xí)第一個(gè)庫(kù):urllib

我們先從urllib開(kāi)始學(xué)習(xí)吧。
既然是爬蟲(chóng)。我們就有個(gè)抓取的目標(biāo)啊。我做了一個(gè)頁(yè)面,可以作為我們抓取的目標(biāo)來(lái)練習(xí)。在這個(gè)頁(yè)面查你可以查ip代理、ip物理地址、或是headers偽裝是否有效、cookies是否添加成功、get請(qǐng)求、post請(qǐng)求等。后面我們會(huì)繼續(xù)用這個(gè)頁(yè)面測(cè)試。

http://www.tenliu.top/index.php/httpclient/

urllib常用方法

urllib.urlopen()

urllib.urlopen(url[, data][, proxies])

三個(gè)參數(shù):

  • url:url字符串
  • data:[可選],請(qǐng)求參數(shù)
  • proxies:[可選],代理ip

urllib.urlencode()

對(duì)dict類型進(jìn)行url編碼

urllib.quote()

對(duì)字符串進(jìn)行url編碼

在例子中實(shí)踐

最基本爬蟲(chóng)

直接上代碼啦

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib
import urllib2
import requests
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
def first():
    #最基本的
    url = "http://www.tenliu.top/index.php/httpclient/"
    resp = urllib.urlopen(url)
    #頁(yè)面源碼
    print resp.read()
    #返回的頭信息
    print resp.info()
    #返回的http狀態(tài)碼
    print resp.getcode()
    #響應(yīng)的url
    print resp.geturl()
if __name__=="__main__":
    first()

執(zhí)行一下看看,這個(gè)頁(yè)面就抓下來(lái)了,看其實(shí)爬蟲(chóng)就是這么簡(jiǎn)單···

get請(qǐng)求提交數(shù)據(jù)

urlencode()

請(qǐng)求頁(yè)面也是可以帶參數(shù)訪問(wèn)的:
http://www.tenliu.top/index.php/httpclient?country=%E4%B8%AD%E5%9B%BD&ranking=2
點(diǎn)擊訪問(wèn)看看

現(xiàn)在有個(gè)問(wèn)題,我們看到在瀏覽器的地址欄中參數(shù)明明顯示如下

http://www.tenliu.top/index.php/httpclient?country=中國(guó)&ranking=2

為什么復(fù)制出來(lái),漢字“中國(guó)”被編碼了呢?
這就是url編碼,其目的是什么呢?我們知道url的參數(shù)解析,會(huì)按照規(guī)定的特殊字符進(jìn)行解析,例如不同參數(shù)之間使用"&"分割,如果一個(gè)參數(shù)值當(dāng)中含有這些特殊的字符,豈不是讓url解析變得混亂。因此需要url編碼。urllib中提供了url編碼的函數(shù):urlencode()和quote()

  • urllib.urlencode()??梢詫?duì)dict進(jìn)行url編碼。這個(gè)函數(shù)似乎就是urllib存在的意義了,urllib2中就沒(méi)有這個(gè)函數(shù)。
  • urllib.quote(),對(duì)一般的字符串進(jìn)行url編碼。urllib2中也有這個(gè)函數(shù)啊,urllib2.quote()。
    我們看一下urllib.urlencode()的使用
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib
import urllib2
import requests
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
def thrid_1():
    #傳遞數(shù)據(jù),get請(qǐng)求。對(duì)dict進(jìn)行url編碼,
    data = {'country': '中國(guó)', 'ranking': 2}
    params = urllib.urlencode(data)
    url = "http://www.tenliu.top/index.php/httpclient" +"?"+ params
    print url
    resp = urllib.urlopen(url)
    print resp.read()
if __name__=="__main__":
    thrid_1()

quote()

那么urllib.quote()使用場(chǎng)景是什么呢?例如如下頁(yè)面
http://www.tenliu.top/index.php/httpclient/?query=%E4%B8%AD%E5%9B%BD%202018%20%E8%8A%82%E5%81%87%E6%97%A5
在搜索頁(yè)面很常見(jiàn)的url,比如百度搜索,url參數(shù)是對(duì)query的url編碼

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib
import urllib2
import requests
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
def thrid_2():
    #傳遞數(shù)據(jù),get請(qǐng)求。urllib.quote對(duì)字符串進(jìn)行url編碼。urllib2.quote()有同樣的函數(shù)。
    url = "http://www.tenliu.top/index.php/httpclient?query="+ urllib.quote("中國(guó) 2018 節(jié)假日")
    print url
    resp = urllib.urlopen(url)
    print resp.read()
if __name__=="__main__":
    thrid_2()

post請(qǐng)求提交數(shù)據(jù)

post方式提交數(shù)據(jù)的頁(yè)面,爬蟲(chóng)要怎么抓取。post請(qǐng)求提交的參數(shù),并不表現(xiàn)在鏈接中,可以抓包看看,提交的參數(shù)k/v都是什么。
http://www.tenliu.top/index.php/httpclient/
這個(gè)頁(yè)面中有個(gè)表單,可以post提交數(shù)據(jù),隨便填寫(xiě)內(nèi)容,post提交吧。
這個(gè)過(guò)程我們抓包分析一下看看
【爬蟲(chóng)-抓包分析】

如何抓包請(qǐng)看
爬蟲(chóng)-抓包分析
例如我填寫(xiě)小明和123,抓包可以看到form表單提交的數(shù)據(jù)如下:

login_name:小明
login_pwd:123

urllib.urlopen()支持post請(qǐng)求,用法如下:
urllib.urlopen(url,data)
當(dāng)data為空是,urlopen發(fā)送的就是get請(qǐng)求,data有值時(shí),發(fā)送的就是post請(qǐng)求。
爬蟲(chóng)代碼如下

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib
import urllib2
import requests
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
def fourth():
    #傳遞數(shù)據(jù),post請(qǐng)求
    data = {'login_name': '小明', 'login_pwd': '123'}
    params = urllib.urlencode(data)
    url = "http://www.tenliu.top/index.php/httpclient"
    resp = urllib.urlopen(url, params)
    print resp.read()
if __name__=="__main__":
    fourth()

添加ip代理

很多網(wǎng)站又反扒策略,當(dāng)一個(gè)ip過(guò)于頻繁的請(qǐng)求這個(gè)網(wǎng)站是,這個(gè)ip有可能就會(huì)被這個(gè)網(wǎng)站封掉,一段時(shí)間這個(gè)ip是無(wú)法訪問(wèn)該網(wǎng)站。
或是在大陸國(guó)外一些資源是訪問(wèn)不了的,爬蟲(chóng)加上國(guó)外的ip代理,就可以抓取了。
urllib.urlopen()支持ip代理設(shè)置:
urllib.urlopen(url,proxies)
代碼:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib
import urllib2
import requests
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
def second():
    url = "http://www.tenliu.top/index.php/httpclient?k1=v1&k2=v2"
    proxies = {'http': 'http://www.someproxy.com:3128'}
    resp = urllib.urlopen(url, proxies=proxies)
    print resp.read()
if __name__=="__main__":
    second()

總結(jié)

以上基本覆蓋了urllib的爬蟲(chóng)方法?,F(xiàn)在我們?cè)倏纯磚rllib的缺點(diǎn):
1、最大的問(wèn)題就是無(wú)法偽裝,headers無(wú)法設(shè)置這樣寫(xiě)的爬蟲(chóng)很容易被發(fā)現(xiàn)進(jìn)而被ban啊,并且cookies也沒(méi)法帶
2、沒(méi)有session,登錄狀態(tài)怎么保持
3、···
基于此,python基金會(huì)不得不推出urllib2作為urllib的補(bǔ)充版或增強(qiáng)版,而urllib2的表現(xiàn)如何呢,我們下次介紹

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容