scrapy框架之CrawlSpider全站自動爬取

全站數據爬取的方式php

  1.經過遞歸的方式進行深度和廣度爬取全站數據,可參考相關博文(全站圖片爬取),手動藉助scrapy.Request模塊發起請求。html

  2.對於必定規則網站的全站數據爬取,能夠使用CrawlSpider實現自動爬取。dom

CrawlSpider是基於Spider的一個子類。和蜘蛛同樣,都是scrapy裏面的一個爬蟲類,但 CrawlSpider是蜘蛛的子類,子類要比父類功能多,它有本身的都有功能------ 提取連接的功能LinkExtractor(連接提取器)。Spider是全部爬蟲的基類,其設計原則只是爲了爬取start_url列表中網頁,而從爬取到的網頁中提取出的url進行繼續的爬取工做使用CrawlSpider更合適。scrapy

項目建立ide

#建立工程項目:項目名CrawlSpiderPro可自定義
scrapy startproject CrawlSpiderPro
#切換到當前工程目錄下
cd CrawlSpiderPro
#建立爬蟲文件,比普通的爬蟲文件多了參數「-t crawl」
scrapy genspider -t crawl crawlSpiderTest www.xxx.com
#開啓爬蟲項目
scrapy crawl crawlSpiderTest

初始化爬蟲文件解析  函數

 1 class CrawlspidertestSpider(CrawlSpider):
 2     name = 'crawlSpiderTest'
 3     allowed_domains = ['www.xxx.com']
 4     start_urls = ['http://www.xxx.com/']
 5     #爬蟲規則rules指定不一樣的規則解析器,一個Rule就是一個解析規則,能夠定義多個
 6     rules = (
 7         #Rule是規則解析器;
 8         # LinkExtractor是鏈接提取器,提取符合allow規則的完整的url;
 9         #callback指定當前規則解析器的回調解析函數;
10         #follow指定是否將連接提取器繼續做用到連接提取器提取出的連接網頁;
11         Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
12     )
13 
14     def parse_item(self, response):
15         item = {}
16         #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
17         #item['name'] = response.xpath('//div[@id="name"]').get()
18         #item['description'] = response.xpath('//div[@id="description"]').get()
19         return item

東莞陽光網(http://wz.sun0769.com/index.php/question/report?page=)全站爬取案例:網站

  1.爬蟲腳本crawlSpiderTest.pyurl

 1 # -*- coding: utf-8 -*-
 2 import scrapy
 3 from scrapy.linkextractors import LinkExtractor
 4 from scrapy.spiders import CrawlSpider, Rule
 5 from CrawlSpiderPro.items import CrawlspiderproItem
 6 
 7 
 8 class CrawlspidertestSpider(CrawlSpider):
 9     name = 'crawlSpiderTest'
10     # allowed_domains = ['www.xxx.com']
11 
12     start_urls = ['http://wz.sun0769.com/index.php/question/report?page=']
13     #爬蟲規則rules指定不一樣的規則解析器,一個Rule就是一個解析規則,能夠定義多個
14     rules = (
15         #Rule是規則解析器;
16         # LinkExtractor是鏈接提取器,提取符合allow規則的完整的url;
17         #callback指定當前規則解析器的回調解析函數;
18         #follow指定是否將連接提取器繼續做用到連接提取器提取出的連接網頁;
19         #follow不指定默認False;
20         Rule(LinkExtractor(allow=r'page=\d+'), callback='parse_item', follow=False),#提取頁碼
21         Rule(LinkExtractor(allow=r'question/\d+/\d+.shtml'), callback='parse_detail'),#提取詳細信息頁面
22     )
23 
24     def parse_item(self, response):
25         print(response)
26         item = CrawlspiderproItem()
27         tr_list=response.xpath('//*[@id="morelist"]/div/table[2]/tbody/tr/td/table/tbody/tr')
28 
29         for tr in tr_list:
30             item['identifier']=tr.xpath('./td[1]/text()').extract_first()#解析編號
31             item['title']=tr.xpath('/td[2]/a[2]/text()').extract_first()#解析標題
32             yield item
33 
34     def parse_detail(self, response):
35         print(12345678765)
36         item = CrawlspiderproItem()
37         #xpath解析不識別tbody
38         item['identifier']=response.xpath('/html/body/div[9]/table[1]/tr/td[2]/span[2]/text()').extract_first().split(':')[-1]
39         item['content']="".join(response.xpath('/html/body/div[9]/table[2]//text()').extract())
40 
41         yield item
crawlSpiderTest.py

  2.itmes.py字段屬性定義spa

 1 import scrapy
 2 
 3 
 4 #也能夠定義兩個類分別存儲,最後在和管道經過編號字段進行彙總對應,而後持久化存儲
 5 class CrawlspiderproItem(scrapy.Item):
 6    
 7     #編號
 8     identifier=scrapy.Field()
 9     #標題
10     title=scrapy.Field()
11     #內容
12     content=scrapy.Field()
13     pass
itmes.py

  3.pipelines.py管道配置設計

1 #自定義持久化處理
2 class CrawlspiderproPipeline(object):
3     def process_item(self, item, spider):
4         print(item)
5         return item
pipelines.py

  4.settings.py配置

#UA假裝
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
#robots協議
ROBOTSTXT_OBEY = False
#日誌輸出等級
LOG_LEVEL='ERROR'

#開啓管道
ITEM_PIPELINES = {
   'CrawlSpiderPro.pipelines.CrawlspiderproPipeline': 300,
}
相關文章
相關標籤/搜索