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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python爬蟲(chóng)學(xué)習(xí)筆記之正則表達(dá)式
Python爬蟲(chóng)學(xué)習(xí)筆記之正則表達(dá)式
2018-04-28
收藏

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

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

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

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

這個(gè)時(shí)候的輸出結(jié)果是 ['', '', '', '', '', 'w', '', '', '', '', '', '', '', '', '', 'w', '', '', '', '', ''],可見(jiàn)是一個(gè)列表,長(zhǎng)度和匹配的字符串一致,遇到要匹配的字符就打印出來(lái)。

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

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

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

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

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

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

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

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

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

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


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

然后就可以利用我們上述的知識(shí)去提取我們想要的信息,源代碼如下

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

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

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

好了介紹完了,我們?nèi)タ聪逻\(yùn)行結(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

這個(gè)時(shí)候就下載成功了,到我們的 picture 目錄下去查看下載的圖片

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



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

若不方便掃碼,搜微信號(hào):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(), // 加隨機(jī)數(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)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }