自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Pyspider框架 —— Python爬蟲實戰(zhàn)之爬取 V2EX 網(wǎng)站帖子

開發(fā) 后端
一個國人編寫的強大的網(wǎng)絡爬蟲系統(tǒng)并帶有強大的WebUI。采用Python語言編寫,分布式架構,支持多種數(shù)據(jù)庫后端,強大的WebUI支持腳本編輯器,任務監(jiān)視器,項目管理器以及結果查看器。

背景:

PySpider:一個國人編寫的強大的網(wǎng)絡爬蟲系統(tǒng)并帶有強大的WebUI。采用Python語言編寫,分布式架構,支持多種數(shù)據(jù)庫后端,強大的WebUI支持腳本編輯器,任務監(jiān)視器,項目管理器以及結果查看器。在線示例: http://demo.pyspider.org/

官方文檔: http://docs.pyspider.org/en/l...

Github : https://github.com/binux/pysp...

本文爬蟲代碼 Github 地址:https://github.com/zhisheng17...

更多精彩文章可以在微信公眾號:猿blog 閱讀到,歡迎關注。

說了這么多,我們還是來看正文吧!

前提:

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

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

我所遇到的一些錯誤:

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

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

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

創(chuàng)建一個項目

在 pyspider 的 dashboard 的右下角,點擊 “Create” 按鈕

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

  1. @every(minutes=24 * 60) 
  2.     def on_start(self): 
  3.         self.crawl('https://www.v2ex.com/', callback=self.index_page, validate_cert=False 
  • self.crawl 告訴 pyspider 抓取指定頁面,然后使用 callback 函數(shù)對結果進行解析。
  • @every) 修飾器,表示 on_start 每天會執(zhí)行一次,這樣就能抓到***的帖子了。
  • validate_cert=False 一定要這樣,否則會報 HTTP 599: SSL certificate problem: unable to get local issuer certificate錯誤

首頁:

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

 

 

 

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

Tab 列表頁 :

 

 

 

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

代碼:

  1. @config(age=10 * 24 * 60 * 60) 
  2.     def index_page(self, response): 
  3.         for each in response.doc('a[href^="https://www.v2ex.com/?tab="]').items(): 
  4.             self.crawl(each.attr.href, callback=self.tab_page, validate_cert=False 
  • 由于帖子列表頁和 tab列表頁長的并不一樣,在這里新建了一個 callback 為 self.tab_page
  • @config(age=10 24 60 * 60) 在這表示我們認為 10 天內頁面有效,不會再次進行更新抓取

Go列表頁 :

 代碼:

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

帖子詳情頁(T):

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

同時我們還需要讓他自己實現(xiàn)自動翻頁功能。

代碼: 

  1. @config(age=10 * 24 * 60 * 60) 
  2.     def board_page(self, response): 
  3.         for each in response.doc('a[href^="https://www.v2ex.com/t/"]').items(): 
  4.             url = each.attr.href 
  5.             if url.find('#reply')>0: 
  6.                 url = url[0:url.find('#')] 
  7.             self.crawl(url, callback=self.detail_page, validate_cert=False
  8.         for each in response.doc('a.page_normal').items(): 
  9.             self.crawl(each.attr.href, callback=self.board_page, validate_cert=False) #實現(xiàn)自動翻頁功能  

去掉后的運行截圖:

實現(xiàn)自動翻頁后的截圖:

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

點擊每個帖子后面的按鈕就可以查看帖子具體詳情了。

 代碼:

  1. @config(priority=2) 
  2.     def detail_page(self, response): 
  3.         title = response.doc('h1').text() 
  4.         content = response.doc('div.topic_content').html().replace('"''\\"'
  5.         self.add_question(title, content)  #插入數(shù)據(jù)庫 
  6.         return { 
  7.             "url": response.url, 
  8.             "title": title, 
  9.             "content": content, 
  10.         } 

 插入數(shù)據(jù)庫的話,需要我們在之前定義一個add_question函數(shù)。

  1. #連接數(shù)據(jù)庫 
  2. def __init__(self): 
  3.         self.db = MySQLdb.connect('localhost''root''root''wenda', charset='utf8'
  4.  
  5.     def add_question(self, title, content): 
  6.         try: 
  7.             cursor = self.db.cursor() 
  8.             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ù)庫的SQL語句 
  9.             print sql 
  10.             cursor.execute(sql) 
  11.             print cursor.lastrowid 
  12.             self.db.commit() 
  13.         except Exception, e: 
  14.             print e 
  15.             self.db.rollback()  

查看爬蟲運行結果:

先debug下,再調成running。pyspider框架在windows下的bug

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

查看運行工作

查看爬取下來的內容

  

 

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

自己需要用的話就可以導入出來了。

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

當然你還會看到其他的爬蟲代碼的了,如果你覺得不錯可以給個 Star,或者你也感興趣的話,你可以fork我的項目,和我一起學習,這個項目長期更新下去。

***:

代碼:

  1. # created by 10412 
  2. # !/usr/bin/env python 
  3. # -*- encoding: utf-8 -*- 
  4. # Created on 2016-10-20 20:43:00 
  5. # Project: V2EX 
  6.  
  7. from pyspider.libs.base_handler import * 
  8.  
  9. import re 
  10. import random 
  11. import MySQLdb 
  12.  
  13. class Handler(BaseHandler): 
  14.     crawl_config = { 
  15.     } 
  16.  
  17.     def __init__(self): 
  18.         self.db = MySQLdb.connect('localhost''root''root''wenda', charset='utf8'
  19.  
  20.     def add_question(self, title, content): 
  21.         try: 
  22.             cursor = self.db.cursor() 
  23.             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()'); 
  24.             print sql 
  25.             cursor.execute(sql) 
  26.             print cursor.lastrowid 
  27.             self.db.commit() 
  28.         except Exception, e: 
  29.             print e 
  30.             self.db.rollback() 
  31.  
  32.  
  33.     @every(minutes=24 * 60) 
  34.     def on_start(self): 
  35.         self.crawl('https://www.v2ex.com/', callback=self.index_page, validate_cert=False
  36.  
  37.     @config(age=10 * 24 * 60 * 60) 
  38.     def index_page(self, response): 
  39.         for each in response.doc('a[href^="https://www.v2ex.com/?tab="]').items(): 
  40.             self.crawl(each.attr.href, callback=self.tab_page, validate_cert=False
  41.  
  42.  
  43.     @config(age=10 * 24 * 60 * 60) 
  44.     def tab_page(self, response): 
  45.         for each in response.doc('a[href^="https://www.v2ex.com/go/"]').items(): 
  46.             self.crawl(each.attr.href, callback=self.board_page, validate_cert=False
  47.  
  48.  
  49.     @config(age=10 * 24 * 60 * 60) 
  50.     def board_page(self, response): 
  51.         for each in response.doc('a[href^="https://www.v2ex.com/t/"]').items(): 
  52.             url = each.attr.href 
  53.             if url.find('#reply')>0: 
  54.                 url = url[0:url.find('#')] 
  55.             self.crawl(url, callback=self.detail_page, validate_cert=False
  56.         for each in response.doc('a.page_normal').items(): 
  57.             self.crawl(each.attr.href, callback=self.board_page, validate_cert=False
  58.  
  59.  
  60.     @config(priority=2) 
  61.     def detail_page(self, response): 
  62.         title = response.doc('h1').text() 
  63.         content = response.doc('div.topic_content').html().replace('"''\\"'
  64.         self.add_question(title, content)  #插入數(shù)據(jù)庫 
  65.         return { 
  66.             "url": response.url, 
  67.             "title": title, 
  68.             "content": content, 
  69.         }  
責任編輯:龐桂玉 來源: segmentfault
相關推薦

2016-12-07 11:18:58

Python爬蟲網(wǎng)站

2021-03-18 09:18:12

python爬蟲

2017-12-14 21:45:39

2017-05-12 18:00:44

pyspiderAJAXHTTP

2018-05-11 10:16:41

微信小程序反編譯

2016-12-08 16:47:06

2017-05-24 15:07:19

Python爬蟲爬取

2021-06-02 22:18:11

Python關鍵詞微博

2021-01-24 16:40:00

Python爬取網(wǎng)站編程語言

2017-11-20 09:46:08

python爬蟲Scrapy

2020-08-06 08:43:49

Python爬蟲數(shù)據(jù)

2015-08-18 09:20:23

試用期開除員工

2017-05-10 12:05:17

pyspiderHTMLCSS

2021-09-03 09:26:15

Python爬蟲百度百科

2024-08-30 15:03:43

2018-08-08 11:40:24

ScrapyRequest網(wǎng)絡爬蟲

2023-08-30 08:43:42

asyncioaiohttp

2020-10-26 15:09:35

Python爬蟲網(wǎng)頁數(shù)據(jù)

2020-10-16 07:03:17

Scrapy爬蟲框架

2021-06-02 15:10:20

PythonScrapy視頻
點贊
收藏

51CTO技術棧公眾號