最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)公司專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、商南網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為商南等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

背景:

PySpider:一個(gè)國(guó)人編寫的強(qiáng)大的網(wǎng)絡(luò)爬蟲系統(tǒng)并帶有強(qiáng)大的WebUI。采用Python語(yǔ)言編寫,分布式架構(gòu),支持多種數(shù)據(jù)庫(kù)后端,強(qiáng)大的WebUI支持腳本編輯器,任務(wù)監(jiān)視器,項(xiàng)目管理器以及結(jié)果查看器。

前提:

你已經(jīng)安裝好了Pyspider 和 MySQL-python(保存數(shù)據(jù))

如果你還沒安裝的話,請(qǐng)看看我的前一篇文章,防止你也走彎路。

  • Pyspider 框架學(xué)習(xí)時(shí)走過(guò)的一些坑

  • HTTP 599: SSL certificate problem: unable to get local issuer  certificate錯(cuò)誤

我所遇到的一些錯(cuò)誤:

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

首先,本爬蟲目標(biāo):使用 Pyspider 框架爬取 V2EX 網(wǎng)站的帖子中的問題和內(nèi)容,然后將爬取的數(shù)據(jù)保存在本地。

V2EX 中大部分的帖子查看是不需要登錄的,當(dāng)然也有些帖子是需要登陸后才能夠查看的。(因?yàn)楹髞?lái)爬取的時(shí)候發(fā)現(xiàn)一直 error  ,查看具體原因后才知道是需要登錄的才可以查看那些帖子的)所以我覺得沒必要用到 Cookie,當(dāng)然如果你非得要登錄,那也很簡(jiǎn)單,簡(jiǎn)單地方法就是添加你登錄后的  cookie 了。

我們?cè)?https://www.v2ex.com/  掃了一遍,發(fā)現(xiàn)并沒有一個(gè)列表能包含所有的帖子,只能退而求其次,通過(guò)抓取分類下的所有的標(biāo)簽列表頁(yè),來(lái)遍歷所有的帖子:  https://www.v2ex.com/?tab=tech 然后是 https://www.v2ex.com/go/progr... ***每個(gè)帖子的詳情地址是  (舉例): https://www.v2ex.com/t/314683...

創(chuàng)建一個(gè)項(xiàng)目

在 pyspider 的 dashboard 的右下角,點(diǎn)擊 “Create” 按鈕

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

替換 on_start 函數(shù)的 self.crawl 的 URL:

@every(minutes=24 * 60)     def on_start(self):         self.crawl('https://www.v2ex.com/', callback=self.index_page, validate_cert=False)
  • self.crawl 告訴 pyspider 抓取指定頁(yè)面,然后使用 callback 函數(shù)對(duì)結(jié)果進(jìn)行解析。

  • @every) 修飾器,表示 on_start 每天會(huì)執(zhí)行一次,這樣就能抓到***的帖子了。

  • validate_cert=False 一定要這樣,否則會(huì)報(bào) HTTP 599: SSL certificate problem: unable to  get local issuer certificate錯(cuò)誤

首頁(yè):

點(diǎn)擊綠色的 run 執(zhí)行,你會(huì)看到 follows 上面有一個(gè)紅色的 1,切換到 follows 面板,點(diǎn)擊綠色的播放按鈕:

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

 Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

第二張截圖一開始是出現(xiàn)這個(gè)問題了,解決辦法看前面寫的文章,后來(lái)問題就不再會(huì)出現(xiàn)了。

Tab 列表頁(yè) :

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

在 tab 列表頁(yè) 中,我們需要提取出所有的主題列表頁(yè) 的 URL。你可能已經(jīng)發(fā)現(xiàn)了,sample handler 已經(jīng)提取了非常多大的 URL

代碼:

@config(age=10 * 24 * 60 * 60)     def index_page(self, response):         for each in response.doc('a[href^="https://www.v2ex.com/?tab="]').items():             self.crawl(each.attr.href, callback=self.tab_page, validate_cert=False)
  • 由于帖子列表頁(yè)和 tab列表頁(yè)長(zhǎng)的并不一樣,在這里新建了一個(gè) callback 為 self.tab_page

  • @config(age=10 24 60 * 60) 在這表示我們認(rèn)為 10 天內(nèi)頁(yè)面有效,不會(huì)再次進(jìn)行更新抓取

Go列表頁(yè) :

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

代碼:

@config(age=10 * 24 * 60 * 60)  def tab_page(self, response):  for each in response.doc('a[href^="https://www.v2ex.com/go/"]').items():  self.crawl(each.attr.href, callback=self.board_page, validate_cert=False)

帖子詳情頁(yè)(T):

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

你可以看到結(jié)果里面出現(xiàn)了一些reply的東西,對(duì)于這些我們是可以不需要的,我們可以去掉。

同時(shí)我們還需要讓他自己實(shí)現(xiàn)自動(dòng)翻頁(yè)功能。

代碼:

@config(age=10 * 24 * 60 * 60)     def board_page(self, response):         for each in response.doc('a[href^="https://www.v2ex.com/t/"]').items():             url = each.attr.href             if url.find('#reply')>0:                 url = url[0:url.find('#')]             self.crawl(url, callback=self.detail_page, validate_cert=False)         for each in response.doc('a.page_normal').items():             self.crawl(each.attr.href, callback=self.board_page, validate_cert=False) #實(shí)現(xiàn)自動(dòng)翻頁(yè)功能

去掉后的運(yùn)行截圖:

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

實(shí)現(xiàn)自動(dòng)翻頁(yè)后的截圖:

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

此時(shí)我們已經(jīng)可以匹配了所有的帖子的 url 了。

點(diǎn)擊每個(gè)帖子后面的按鈕就可以查看帖子具體詳情了。

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

代碼:

@config(priority=2)     def detail_page(self, response):         title = response.doc('h2').text()         content = response.doc('div.topic_content').html().replace('"', '\\"')         self.add_question(title, content)  #插入數(shù)據(jù)庫(kù)         return {             "url": response.url,             "title": title,             "content": content,         }

插入數(shù)據(jù)庫(kù)的話,需要我們?cè)谥岸x一個(gè)add_question函數(shù)。

#連接數(shù)據(jù)庫(kù) def __init__(self):         self.db = MySQLdb.connect('localhost', 'root', 'root', 'wenda', charset='utf8')      def add_question(self, title, content):         try:             cursor = self.db.cursor()             sql = 'insert into question(title, content, user_id, created_date, comment_count) values ("%s","%s",%d, %s, 0)' % (title, content, random.randint(1, 10) , 'now()');   #插入數(shù)據(jù)庫(kù)的SQL語(yǔ)句             print sql             cursor.execute(sql)             print cursor.lastrowid             self.db.commit()         except Exception, e:             print e             self.db.rollback()

查看爬蟲運(yùn)行結(jié)果:

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

先debug下,再調(diào)成running。pyspider框架在windows下的bug

設(shè)置跑的速度,建議不要跑的太快,否則很容易被發(fā)現(xiàn)是爬蟲的,人家就會(huì)把你的IP給封掉的

查看運(yùn)行工作

查看爬取下來(lái)的內(nèi)容

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子

然后再本地?cái)?shù)據(jù)庫(kù)GUI軟件上查詢下就可以看到數(shù)據(jù)已經(jīng)保存到本地了。

自己需要用的話就可以導(dǎo)入出來(lái)了。

在開頭我就告訴大家爬蟲的代碼了,如果詳細(xì)的看看那個(gè)project,你就會(huì)找到我上傳的爬取數(shù)據(jù)了。(僅供學(xué)習(xí)使用,切勿商用!)

當(dāng)然你還會(huì)看到其他的爬蟲代碼的了,如果你覺得不錯(cuò)可以給個(gè)  Star,或者你也感興趣的話,你可以fork我的項(xiàng)目,和我一起學(xué)習(xí),這個(gè)項(xiàng)目長(zhǎng)期更新下去。

代碼:

# created by 10412 # !/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2016-10-20 20:43:00 # Project: V2EX  from pyspider.libs.base_handler import *  import re import random import MySQLdb  class Handler(BaseHandler):     crawl_config = {     }      def __init__(self):         self.db = MySQLdb.connect('localhost', 'root', 'root', 'wenda', charset='utf8')      def add_question(self, title, content):         try:             cursor = self.db.cursor()             sql = 'insert into question(title, content, user_id, created_date, comment_count) values ("%s","%s",%d, %s, 0)' % (title, content, random.randint(1, 10) , 'now()');             print sql             cursor.execute(sql)             print cursor.lastrowid             self.db.commit()         except Exception, e:             print e             self.db.rollback()       @every(minutes=24 * 60)     def on_start(self):         self.crawl('https://www.v2ex.com/', callback=self.index_page, validate_cert=False)      @config(age=10 * 24 * 60 * 60)     def index_page(self, response):         for each in response.doc('a[href^="https://www.v2ex.com/?tab="]').items():             self.crawl(each.attr.href, callback=self.tab_page, validate_cert=False)       @config(age=10 * 24 * 60 * 60)     def tab_page(self, response):         for each in response.doc('a[href^="https://www.v2ex.com/go/"]').items():             self.crawl(each.attr.href, callback=self.board_page, validate_cert=False)       @config(age=10 * 24 * 60 * 60)     def board_page(self, response):         for each in response.doc('a[href^="https://www.v2ex.com/t/"]').items():             url = each.attr.href             if url.find('#reply')>0:                 url = url[0:url.find('#')]             self.crawl(url, callback=self.detail_page, validate_cert=False)         for each in response.doc('a.page_normal').items():             self.crawl(each.attr.href, callback=self.board_page, validate_cert=False)       @config(priority=2)     def detail_page(self, response):         title = response.doc('h2').text()         content = response.doc('div.topic_content').html().replace('"', '\\"')         self.add_question(title, content)  #插入數(shù)據(jù)庫(kù)         return {             "url": response.url,             "title": title,             "content": content,         }

關(guān)于Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。


當(dāng)前文章:Pyspider框架中Python如何爬取V2EX網(wǎng)站帖子
文章出自:http://fisionsoft.com.cn/article/igjpge.html