99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python 爬蟲學(xué)習(xí)筆記之正則表達(dá)式
Python 爬蟲學(xué)習(xí)筆記之正則表達(dá)式
2017-09-03
收藏

Python 爬蟲學(xué)習(xí)筆記之正則表達(dá)式

正則表達(dá)式是用來匹配字符串非常強大的工具,在其他編程語言中同樣有正則表達(dá)式的概念,Python同樣不例外,利用了正則表達(dá)式,我們想要從返回的頁面內(nèi)容提取出我們想要的內(nèi)容就易如反掌了。

正則表達(dá)式的使用
想要學(xué)習(xí) Python 爬蟲 , 首先需要了解一下正則表達(dá)式的使用,下面我們就來看看如何使用。
. 的使用這個時候的點就相當(dāng)于一個占位符,可以匹配任意一個字符,什么意思呢?看個例子就知道    
import re
content = "helloworld"
b = re.findall('w.',content)
print b`

注意了,我們首先導(dǎo)入了 re,這個時候大家猜一下輸出結(jié)果是什么?因為 . 相當(dāng)于一個占位符,所以理所當(dāng)然的這個時候的輸出結(jié)果是 wo 。

* 的使用跟上面的 . 不同,* 可以匹配前一個字符任意次數(shù),看個例子    
content = "helloworldhelloworld"
b = re.findall('w*',content)
print b

這個時候的輸出結(jié)果是 ['', '', '', '', '', 'w', '', '', '', '', '', '', '', '', '', 'w', '', '', '', '', ''],可見是一個列表,長度和匹配的字符串一致,遇到要匹配的字符就打印出來。

.* 的使用.* 是一種組合使用,它可以盡可能多的匹配內(nèi)容,比如下面這個例子    
content = "helloworldhelloworldworld"
b = re.findall('he.*ld',content)
print b

它會輸出 ['helloworldhelloworldworld'],它為什么不只打印一個 helloworld,為什么全部打印下來了?這就是一種貪心算法,也就是說我要找到最長的那個符合條件的內(nèi)容。

.*? 的使用與 上面相反,這個符號會找到盡可能短的符合條件的內(nèi)容,然后放到一個列表中去,如下所示    
content = 'xxhelloworldxxxxhelloworldxx'
b = re.findall('xx.*?xx',content)
print b

輸出的結(jié)果為 ['xxhelloworldxx', 'xxhelloworldxx'],可見,有個 xx 在前面好煩,怎么才能去掉呢?很簡單,加個括號即可,括號加在哪?    
content = 'xxhelloworldxxxxhelloworldxx'
b = re.findall('xx(.*?)xx',content)
print b

以上我們討論的都是內(nèi)容不包含換行符的情況,如果有了換行符結(jié)果又會發(fā)生什么變化呢?    
content = '''xxhelloworld xx'''
b = re.findall('xx(.*?)xx',content)
print b

這個時候的輸出結(jié)果為一個空列表,那怎么辦啊?如果我們寫網(wǎng)絡(luò)爬蟲的時候,網(wǎng)頁源代碼肯定不止是一行啊,如果換一行我們就讀不出來了,那就好尷尬了,當(dāng)然有解決辦法~    
content = '''xxhelloworld xx'''
b = re.findall('xx(.*?)xx',content,re.S)
print b

這樣就可以了,還有一個非常方便的提取數(shù)字的技巧,如下所示    
content = '''xx123456 xx'''
b = re.findall('(d+)',content,re.S)
print b

在網(wǎng)頁源代碼中爬取圖片鏈接并下載

這篇文章中只是網(wǎng)絡(luò)爬蟲的第一步,所以講解的也比較淺,所以現(xiàn)在我們先來利用正則表達(dá)式實現(xiàn)一個手動的網(wǎng)絡(luò)爬蟲,什么是手動的呢?就是我們自己把網(wǎng)頁源代碼復(fù)制下來,保存在一個 txt 文件中,然后利用正則表達(dá)式去過濾信息,然后去下載。

首先我搜索了一下 Linux 桌面,然后找到了如下一個網(wǎng)頁

右擊查看網(wǎng)絡(luò)源代碼,按 ctrl+f 搜索 img src 找到中間一部分進(jìn)行復(fù)制,并且粘貼到一個 txt 文件中去,

然后就可以利用我們上述的知識去提取我們想要的信息,源代碼如下    
import re import requests
 f = open('source.txt', 'r')
 html = f.read()
 f.close()
 pattern = '<img src="(.*?)"'
 pic_url = re.findall(pattern, html, re.S)
 i = 0
 for each in pic_url:
   print 'Downloading :' + each
   pic = requests.get(each)
   fp = open('picture\\' + str(i) + '.jpg', 'wb')
   fp.write(pic.content)
   fp.close()
   i = i + 1

首先打開我們保存網(wǎng)絡(luò)源代碼的 txt文件,進(jìn)行讀取,關(guān)閉文件流,然后就是利用正則表達(dá)式提取圖片鏈接,最后利用requests 中的 get() 方法進(jìn)行圖片下載,注意這個 requests 不是Python 中自帶的,我們需要下載指定的文件,然后將其放入到 Python 的Lib 目錄下,此處下載,進(jìn)入網(wǎng)站后,按ctrl+f 搜索關(guān)鍵詞 requests 就可以看到如下頁面

,可以看出,我們下載的是 .whl 后綴的文件,手動將其改成 .zip 后綴,然后解壓,就可以得到兩個目錄,將名為 requests 的目錄復(fù)制粘貼到上面講的目錄即可使用。

好了介紹完了,我們?nèi)タ聪逻\行結(jié)果    
C:Python27python.exe E:/PythonCode/20160820/Spider.py
Downloading:http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112732422680200576.JPG
Downloading :http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112640070563900918.JPG
Downloading :http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112547718465744154.JPG
Downloading :http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112455366330382227.JPG
Downloading :http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112363014254719641.JPG
Downloading :http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112270662197888742.JPG
Downloading :http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112178310031994750.JPG
Downloading :http://n1.itc.cn/img8/wb/smccloud/fetch/2015/07/04/112085957910403853.JPG
 
Process finished with exit code 0

這個時候就下載成功了,到我們的 picture 目錄下去查看下載的圖片

下載成功了。注意,自己找網(wǎng)頁源代碼實驗的時候,最好不要讓鏈接中帶有中文,否則可能會出現(xiàn)亂碼,由于我本身學(xué)習(xí) Python 也才很短的時間,關(guān)于中文亂碼問題,應(yīng)對起來還不是那么得心應(yīng)手,所以在此也就不再講解,本文暫時告以段落,有意見或疑問可留言或者私聊我。


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }