摘要:無(wú)意中發(fā)現(xiàn)貼吧也出了個(gè)漂流瓶的東西,隨手翻了翻發(fā)現(xiàn)居然有好多妹子圖,閑來(lái)無(wú)事于是就想寫個(gè)爬蟲(chóng)程序把圖片全部抓取下來(lái)。具體獲取一頁(yè)內(nèi)容的如下看參數(shù)很容易明白,就是當(dāng)前頁(yè)碼,就是當(dāng)前頁(yè)中包含的漂流瓶數(shù)量。
無(wú)意中發(fā)現(xiàn)貼吧也出了個(gè)漂流瓶的東西,隨手翻了翻發(fā)現(xiàn)居然有好多妹子圖,閑來(lái)無(wú)事于是就想寫個(gè)爬蟲(chóng)程序把圖片全部抓取下來(lái)。
這里是貼吧漂流瓶地址
http://tieba.baidu.com/bottle...
首先打開(kāi)抓包神器 Fiddler ,然后打開(kāi)漂流瓶首頁(yè),加載幾頁(yè)試試,在Fiddler中過(guò)濾掉圖片數(shù)據(jù)以及非 http 200 狀態(tài)碼的干擾數(shù)據(jù)后,發(fā)現(xiàn)每一頁(yè)的數(shù)據(jù)獲取都很有規(guī)律,這就給抓取提供了便利。具體獲取一頁(yè)內(nèi)容的url如下:
http://tieba.baidu.com/bottle...
看參數(shù)很容易明白,page_number 就是當(dāng)前頁(yè)碼,page_size 就是當(dāng)前頁(yè)中包含的漂流瓶數(shù)量。
訪問(wèn)后得到的是一個(gè)json格式的數(shù)據(jù),結(jié)構(gòu)大致如下:
{ "error_code": 0, "error_msg": "success", "data": { "has_more": 1, "bottles": [ { "thread_id": "5057974188", "title": "美得不可一世", "img_url": "http://imgsrc.baidu.com/forum/pic/item/a8c87dd062d9f2d3f0113c2ea0ec8a136227cca9.jpg" }, { "thread_id": "5057974188", "title": "美得不可一世", "img_url": "http://imgsrc.baidu.com/forum/pic/item/a8c87dd062d9f2d3f0113c2ea0ec8a136227cca9.jpg" }, ... } }
內(nèi)容很直白一眼就看出,bottles 中的數(shù)據(jù)就是我們想要的(thread_id 瓶子具體id, title 妹紙吐槽的內(nèi)容, img_url 照片真實(shí)地址),遍歷 bottles 就可以獲得當(dāng)前頁(yè)的所有漂流瓶子。(其實(shí)現(xiàn)在得到的只是封面圖哦,打開(kāi)具體的瓶子有驚喜,因?yàn)槲冶容^懶就懶得寫了,不過(guò)我也分析了內(nèi)部的數(shù)據(jù),具體url是:http://tieba.baidu.com/bottle...瓶子thread_id>)
還有一個(gè)參數(shù) has_more 猜測(cè)是是否存在下一頁(yè)的意思。
到這里采集方式應(yīng)該可以確定了。就是從第一頁(yè)不停往后循環(huán)采集,直到 has_more 這個(gè)參數(shù)不為 1 結(jié)束。
這里采用的是 python2.7 + urllib2 + demjson 來(lái)完成此項(xiàng)工作。urllib2 是python2.7自帶的庫(kù),demjson 需要自己安裝下(一般情況下用python自帶的json庫(kù)就可以完成json解析任務(wù),但是現(xiàn)在好多網(wǎng)站提供的json并不規(guī)范,這就讓自帶json庫(kù)無(wú)能為力了。)
demjson 安裝方式 (windows 不需要 sudo)
sudo pip install demjson
或者
2.1獲得一頁(yè)內(nèi)容sudo esay_install demjson
def bottlegen(): page_number = 1 while True: try: data = urllib2.urlopen( "http://tieba.baidu.com/bottle/bottles?page_number=%d&page_size=30" % page_number).read() json = demjson.decode(data) if json["error_code"] == 0: data = json["data"] has_more = data["has_more"] bottles = data["bottles"] for bottle in bottles: thread_id = bottle["thread_id"] title = bottle["title"] img_url = bottle["img_url"] yield (thread_id, title, img_url) if has_more != 1: break page_number += 1 except: raise print("bottlegen exception") time.sleep(5)
這里使用python的生成器來(lái)源源不斷的輸出分析到的內(nèi)容。
2.2根據(jù)url保存圖片數(shù)據(jù)for thread_id, title, img_url in bottlegen(): filename = os.path.basename(img_url) pathname = "tieba/bottles/%s_%s" % (thread_id, filename) print filename with open(pathname, "wb") as f: f.write(urllib2.urlopen(img_url).read()) f.close()2.3全部代碼如下
# -*- encoding: utf-8 -*- import urllib2 import demjson import time import re import os def bottlegen(): page_number = 1 while True: try: data = urllib2.urlopen( "http://tieba.baidu.com/bottle/bottles?page_number=%d&page_size=30" % page_number).read() json = demjson.decode(data) if json["error_code"] == 0: data = json["data"] has_more = data["has_more"] bottles = data["bottles"] for bottle in bottles: thread_id = bottle["thread_id"] title = bottle["title"] img_url = bottle["img_url"] yield (thread_id, title, img_url) if has_more != 1: break page_number += 1 except: raise print("bottlegen exception") time.sleep(5) def imggen(thread_id): try: data = urllib2.urlopen( "http://tieba.baidu.com/bottle/photopbPage?thread_id=%s" % thread_id).read() match = re.search(r"\_.Module.use("encourage/widget/bottle",(.*?),function(){});", data) data = match.group(1) json = demjson.decode(data) json = demjson.decode(json[1].replace(" ", "")) for i in json: thread_id = i["thread_id"] text = i["text"] img_url = i["img_url"] yield (thread_id, text, img_url) except: raise print("imggen exception") try: os.makedirs("tieba/bottles") except: pass for thread_id, _, _ in bottlegen(): for _, title, img_url in imggen(thread_id): filename = os.path.basename(img_url) pathname = "tieba/bottles/%s_%s" % (thread_id, filename) print filename with open(pathname, "wb") as f: f.write(urllib2.urlopen(img_url).read()) f.close()
運(yùn)行后會(huì)先獲得每頁(yè)所有瓶子,然后再獲得具體瓶子中的所有圖片,輸出到 tieba/bottles/xxxxx.jpg 中。(因?yàn)楸容^懶就沒(méi)做錯(cuò)誤兼容,見(jiàn)諒 ^_^,,,)
結(jié)論結(jié)論是,,, 都是騙人的就首頁(yè)有幾張好看的 - -,,, 他喵的,,,
最后貼下采集成果文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/38580.html
摘要:作為爬蟲(chóng)的入門教程,我想有必要來(lái)個(gè)爬蟲(chóng)程序壓壓驚,爬取性感美女的圖片,然后保存到自己的電腦里面。爽歪歪先看下效果吧,這是我把爬取的圖片自動(dòng)存儲(chǔ)到的文件夾里邊爬蟲(chóng)三步驟抓取,分析,存儲(chǔ)。相關(guān)文章入門基礎(chǔ)有趣的教程 作為 Python 爬蟲(chóng)的入門教程,我想有必要來(lái)個(gè)爬蟲(chóng)程序壓壓驚,爬取性感美女的圖片,然后保存到自己的電腦里面。爽歪歪~ 先看下效果吧,這是我把爬取的圖片自動(dòng)存儲(chǔ)到的文件夾里邊...
摘要:楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)以供學(xué)習(xí),。本文來(lái)源知乎作者路人甲鏈接楚江數(shù)據(jù)提供網(wǎng)站數(shù)據(jù)采集和爬蟲(chóng)軟件定制開(kāi)發(fā)服務(wù),服務(wù)范圍涵蓋社交網(wǎng)絡(luò)電子商務(wù)分類信息學(xué)術(shù)研究等。 楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)以供學(xué)習(xí),http://www.chujiangdata.com。 第一:Python爬蟲(chóng)學(xué)習(xí)系列教程(來(lái)源于某博主:htt...
摘要:我們的目標(biāo)是用爬蟲(chóng)來(lái)干一件略污事情最近聽(tīng)說(shuō)煎蛋上有好多可愛(ài)的妹子,而且爬蟲(chóng)從妹子圖抓起練手最好,畢竟動(dòng)力大嘛。服務(wù)器超載尤其是對(duì)給定服務(wù)器的訪問(wèn)過(guò)高時(shí)。個(gè)人爬蟲(chóng),如果過(guò)多的人使用,可能導(dǎo)致網(wǎng)絡(luò)或者服務(wù)器阻塞。 我們的目標(biāo)是用爬蟲(chóng)來(lái)干一件略污事情 最近聽(tīng)說(shuō)煎蛋上有好多可愛(ài)的妹子,而且爬蟲(chóng)從妹子圖抓起練手最好,畢竟動(dòng)力大嘛。而且現(xiàn)在網(wǎng)絡(luò)上的妹子很黃很暴力,一下接受太多容易營(yíng)養(yǎng)不量,但是本著...
摘要:探探機(jī)器人,自動(dòng)根據(jù)不同妹紙漢子顏值年齡等類型,喜歡忽略,歡迎各位先看一下實(shí)現(xiàn)的結(jié)果吧今天要講的主題是使用腳本實(shí)現(xiàn)你自己想要自動(dòng)操控的任意手機(jī)。 前言 之前寫了篇文章:【全是干貨】談?wù)勅绾螌W(xué)習(xí)一項(xiàng)新技能,沒(méi)有理論,全是實(shí)戰(zhàn),里面第五點(diǎn)提到用腳本玩探探,昨天花了一個(gè)小時(shí)實(shí)現(xiàn)了該功能。 Github:探探機(jī)器人,自動(dòng)根據(jù)不同妹紙/漢子顏值、年齡等類型,喜歡、忽略,歡迎各位star 先看一下...
摘要:時(shí)間永遠(yuǎn)都過(guò)得那么快,一晃從年注冊(cè),到現(xiàn)在已經(jīng)過(guò)去了年那些被我藏在收藏夾吃灰的文章,已經(jīng)太多了,是時(shí)候把他們整理一下了。那是因?yàn)槭詹貖A太亂,橡皮擦給設(shè)置私密了,不收拾不好看呀。 ...
閱讀 2587·2021-11-22 09:34
閱讀 962·2021-11-19 11:34
閱讀 2815·2021-10-14 09:42
閱讀 1499·2021-09-22 15:27
閱讀 2399·2021-09-07 09:59
閱讀 1749·2021-08-27 13:13
閱讀 3442·2019-08-30 11:21
閱讀 784·2019-08-29 18:35