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

熱線電話:13121318867

登錄
首頁精彩閱讀python實現(xiàn)簡單爬蟲功能_python實現(xiàn)簡單爬蟲
python實現(xiàn)簡單爬蟲功能_python實現(xiàn)簡單爬蟲
2016-12-30
收藏

python實現(xiàn)簡單爬蟲功能_python實現(xiàn)簡單爬蟲

在我們?nèi)粘I暇W(wǎng)瀏覽網(wǎng)頁的時候,經(jīng)常會看到一些好看的圖片,我們就希望把這些圖片保存下載,或者用戶用來做桌面壁紙,或者用來做設(shè)計的素材。

我們最常規(guī)的做法就是通過鼠標(biāo)右鍵,選擇另存為。但有些圖片鼠標(biāo)右鍵的時候并沒有另存為選項,還有辦法就通過就是通過截圖工具截取下來,但這樣就降低圖片的清晰度。好吧~!其實你很厲害的,右鍵查看頁面源代碼。

我們可以通過python 來實現(xiàn)這樣一個簡單的爬蟲功能,把我們想要的代碼爬取到本地。下面就看看如何使用python來實現(xiàn)這樣一個功能。

一,獲取整個頁面數(shù)據(jù)

首先我們可以先獲取要下載圖片的整個頁面信息。

getjpg.py

#coding=utf-8 import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html html = getHtml("http://tieba.baidu.com/p/2738151262") print html
  Urllib 模塊提供了讀取web頁面數(shù)據(jù)的接口,我們可以像讀取本地文件一樣讀取www和ftp上的數(shù)據(jù)。首先,我們定義了一個getHtml()函數(shù):

urllib.urlopen()方法用于打開一個URL地址。

read()方法用于讀取URL上的數(shù)據(jù),向getHtml()函數(shù)傳遞一個網(wǎng)址,并把整個頁面下載下來。執(zhí)行程序就會把整個網(wǎng)頁打印輸出。

二,篩選頁面中想要的數(shù)據(jù)

Python 提供了非常強大的正則表達(dá)式,我們需要先要了解一點python 正則表達(dá)式的知識才行。

假如我們百度貼吧找到了幾張漂亮的壁紙,通過到前段查看工具。找到了圖片的地址,如:src=”http://imgsrc.baidu.com/forum……jpg”pic_ext=”jpeg”

修改代碼如下:

import re import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' imgre = re.compile(reg) imglist = re.findall(imgre,html) return imglist html = getHtml("http://tieba.baidu.com/p/2460150866") print getImg(html)
  我們又創(chuàng)建了getImg()函數(shù),用于在獲取的整個頁面中篩選需要的圖片連接。re模塊主要包含了正則表達(dá)式

re.compile() 可以把正則表達(dá)式編譯成一個正則表達(dá)式對象.

re.findall() 方法讀取html 中包含 imgre(正則表達(dá)式)的數(shù)據(jù)。

運行腳本將得到整個頁面中包含圖片的URL地址。

三,將頁面篩選的數(shù)據(jù)保存到本地

把篩選的圖片地址通過for循環(huán)遍歷并保存到本地,代碼如下:

#coding=utf-8 import urllib import re def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' imgre = re.compile(reg) imglist = re.findall(imgre,html) x = 0 for imgurl in imglist: urllib.urlretrieve(imgurl,'%s.jpg' % x) x+=1 html = getHtml("http://tieba.baidu.com/p/2460150866") print getImg(html)
   這里的核心是用到了urllib.urlretrieve()方法,直接將遠(yuǎn)程數(shù)據(jù)下載到本地。

通過一個for循環(huán)對獲取的圖片連接進行遍歷,為了使圖片的文件名看上去更規(guī)范,對其進行重命名,命名規(guī)則通過x變量加1。保存的位置默認(rèn)為程序的存放目錄。

程序運行完成,將在目錄下看到下載到本地的文件。


數(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 進行初始化 // 參數(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); }