之前寫的那個reddit爬取不含細(xì)節(jié),因為我最后爬取沒有采用爬取所有的小板塊,而是采取了從網(wǎng)頁
https://www.reddit.com/r/Wishlist/search?q=flair%3A%27chat%27&restrict_sr=on&sort=new&feature=legacy_search&count=26&before=t3_6omz4l
開始爬取,現(xiàn)在把這個的爬取過程和代碼寫下來,含較多細(xì)節(jié),想直接要數(shù)據(jù)的也可以私信我,不過新手的話爬一下也正好學(xué)習(xí)一下不是嗎?
準(zhǔn)備工作
注:不熟悉scrapy的朋友還是看我上一篇文章推薦的兩個鏈接先熟悉一下。
不熟悉爬取技巧的也推薦看一下
1. 確定爬取的起始網(wǎng)頁

網(wǎng)站的開始頁面為上圖所示。
所以start_url為該頁面的url:
https://www.reddit.com/r/Wishlist/search?q=flair%3A%27chat%27&restrict_sr=on&sort=new&feature=legacy_search&count=26&before=t3_6omz4l
2. 如何獲取下一頁
用Firebug工具使用點擊查看next按鈕這個元素,如下圖所示。

右鍵點擊復(fù)制xpath得到xpath:
/html/body/div[5]/div[4]/div[51]/span/span/a
結(jié)合圖中html中的顯示:
<a href="略" rel="nofollow next">next ?</a>
獲取方式為:
next_page = selector.xpath('//a[@rel="nofollow next"]/@href').extract()
3. 如何獲取圖中每個小塊跳轉(zhuǎn)到的鏈接頁面
以第一個[Chat] August 15, 2017為例:
同樣用Firebug工具使用點擊查看這個元素同時右鍵復(fù)制xpath,得到在
-
在html中的顯示:
<a class="title may-blank " data-event-action="title" href="/r/Wishlist/comments/6tt6vs/chat_august_15_2017/" ...>略</a> # 而跳轉(zhuǎn)到的網(wǎng)頁的url如下 https://www.reddit.com/r/Wishlist/comments/6tt6vs/chat_august_15_2017/
可以看出是 https://www.reddit.com + href中的內(nèi)容組合而成
-
xpath為:
/html/body/div[5]/div[4]/div[1]/div[2]/div[1]/p[1]/a
獲取方式為:
comment_page = selector.xpath('//a[@data-event-action="title"]/@href').extract()
4. 如何獲取界面中的每一組談話
上一步獲取的界面為下圖所示

首先獲取最上面的開始話題的那一句話
xpath為:/html/body/div[5]/div[1]/div[1]/div[2]/div[2]/form/div/div/p
然后獲取下面每一個小塊區(qū)域中的話
第一種方法:
comment_zone = selector.xpath('//div[@data-type="comment"]')
然后
for conversation in comment_zone:
talk = conversation.xpath('//div[@class="usertext-body may-blank-within md-container "]/div/p/text()').extract()
這樣還是不能解決對話的邏輯問題。
第二種方法:
需要更仔細(xì)的觀察html結(jié)構(gòu),可以利用一種深度優(yōu)先搜索的感覺。
先看一下xpath:
最外面:
/html/body/div[5]/div[2]/div[3]/
第一層:
/html/body/div[5]/div[2]/div[3]/+div[*]/+div[2]/form/div/div/p
第二層:
/html/body/div[5]/div[2]/div[3]/+div[*]/+div[3]/div/div[*]/+div[2]/form/div/div/p
第三層:
/html/body/div[5]/div[2]/div[3]/+div[*]/+div[3]/div/div[*]/+div[3]/div/div[*]/+div[2]/form/div/div/p
找到了規(guī)律。
然后結(jié)合在html中的表示,現(xiàn)在貼上代碼,進(jìn)行解釋:
- 獲取每個小塊跳轉(zhuǎn)到的鏈接頁面,然后調(diào)用parse_page

-
然后利用遞歸的思想。
parse_page為入口,負(fù)責(zé)獲取最上面的開始話題的那一句話和調(diào)用parse_zone。
parse_zone為一個遞歸函數(shù),當(dāng)不再有向下的評論時便返回item,item里面存的是對話序列,否則就繼續(xù)調(diào)用,反正就是個深度優(yōu)先搜索的感覺,不過互不影響所以可以并行地爬取。
注:這里需要特別說明的是,meta是一個scrapy用來傳遞參數(shù)的方法,還是比較有用的,傳遞的是一個字典。然后對于正則表達(dá)式和一些編碼的問題,希望讀者也可以自己去耐心了解。def parse_zone(self, response): # 采用遞歸思想,相當(dāng)于一個深度優(yōu)先搜索 pre_path = response.meta['pre_path'][:] pre_comment_list = response.meta['pre_comment_list'][:] now_layer_spec = response.selector.xpath(pre_path +'div[@class="child"]/div[@class="sitetable listing"]/div[*]/div[@class="entry unvoted"]//div[@class="md"]/p[1]').extract() if len(now_layer_spec)>0: i = 1 for spec in now_layer_spec: if len(spec)<1: break next_layer_path = pre_path + 'div[@class="child"]/div[@class="sitetable listing"]/div['+str(i)+']/' now_comment_list = pre_comment_list[:] tmp_conv = re.sub("a href(.*?)</a>",'',str(spec).encode('unicode_escape').strip('<p>').strip('</p>')) now_comment_list.append(tmp_conv) yield scrapy.http.Request(str(response.url), callback=self.parse_zone, meta={'pre_path':next_layer_path[:], 'pre_comment_list':now_comment_list[:]}, dont_filter=True) i += 2 else: item = RedditItem() item['talks'] = pre_comment_list[:] yield item def parse_page(self, response): # 獲取最上面的開始話題的那一句話 first_talk = response.selector.xpath('//div[@id="siteTable"]/div[@data-context="comments"]/div[@class="entry unvoted"]//div[@class="md"]/p[1]').extract() # 獲取下面每一個小塊區(qū)域 comment_zone = response.selector.xpath('//div[@class="commentarea"]/div[@class="sitetable nestedlisting"]/div[*]/div[@class="entry unvoted"]//div[@class="md"]/p[1]').extract() i = 1 for convers in comment_zone: if len(convers) <1: break next_layer_path = '//div[@class="commentarea"]/div[3]/' + 'div['+str(i)+']/' tmp_list=[] tmp_conv1 = re.sub("a href(.*?)</a>",'',str(first_talk[0]).encode('unicode_escape').strip('<p>').strip('</p>')) tmp_list.append(tmp_conv1) tmp_conv2 = re.sub("a href(.*?)</a>",'',str(convers).encode('unicode_escape').strip('<p>').strip('</p>')) tmp_list.append(tmp_conv2) yield scrapy.http.Request(str(response.url), callback=self.parse_zone, meta={'pre_path':next_layer_path[:], 'pre_comment_list':tmp_list[:]}, dont_filter=True) i += 2
自認(rèn)為寫的還算明白,也就不再做過多解釋了。
開始爬取
準(zhǔn)備工作差不多了,剩下的事情就是按照scrapy的框架寫一下其它文件,然后放到服務(wù)器上跑了,至于服務(wù)器的相關(guān)問題可以看我之前的文章。
最近在看神經(jīng)網(wǎng)絡(luò)的事情,所以之前的文章有不全面的也暫時不更新了。