1.深度爬蟲crawlspider
scrapy.spiders.CrawlSpider
創(chuàng)建項目:scrapy startproject <project_name>
創(chuàng)建爬蟲:scrapy genspider -t crawl <spider_name> <domains>
核心處理規(guī)則:from scrapy.spiders import CrawlSpider,Rule
核心處理提雀羝椤:from scrapy.linkextractors import LinkExtractor
2.鏈接提认怼:LinkExtractor
class scrapy.contrib.linkextractor.sgml.SgmlLinkExtractor(
allow = (), #符合正則表達式參數(shù)的數(shù)據(jù)會被提取
deny = (), #符合正則表達式參數(shù)的數(shù)據(jù)會禁止提取
allow_domains = (), #包含在域名中可以提取數(shù)據(jù)
deny_domains = (), #包含在域名中禁止提取數(shù)據(jù)
deny_extensions = (),
restrict_xpath = (), #使用xpath提取數(shù)據(jù),和allow共同起作用
tags = (), #根據(jù)標簽名稱提取數(shù)據(jù)
attrs = (), #根據(jù)標簽屬性提取數(shù)據(jù)
canonicalize = (),
unique = True, #剔除重復鏈接請求
process_value = None
)
classscrapy.contrib.spiders.CrawlSpider
爬取一般網(wǎng)站常用的spider呛牲。其定義了一些規(guī)則(rule)來提供跟進link的方便的機制。 也許該spider并不是完全適合您的特定網(wǎng)站或項目痘番,但其對很多情況都使用核无。 因此您可以以其為起點,根據(jù)需求修改部分方法款违。當然您也可以實現(xiàn)自己的spider唐瀑。
除了從Spider繼承過來的(您必須提供的)屬性外,其提供了一個新的屬性:
rules
一個包含一個(或多個) Rule
對象的集合(list)插爹。 每個 Rule
對爬取網(wǎng)站的動作定義了特定表現(xiàn)哄辣。 Rule對象在下邊會介紹。 如果多個rule匹配了相同的鏈接赠尾,則根據(jù)他們在本屬性中被定義的順序力穗,第一個會被使用。
該spider也提供了一個可復寫(overrideable)的方法:
parse_start_url(response)
當start_url的請求返回時气嫁,該方法被調(diào)用当窗。 該方法分析最初的返回值并必須返回一個 Item
對象或者 一個 Request
對象或者 一個可迭代的包含二者對象。
3.爬取規(guī)則
rules = [
Rule(
link_extractor, # LinkExtractor對象
callback=None, # 請求到響應數(shù)據(jù)時的回調(diào)函數(shù)
cb_kwargs=None, # 調(diào)用函數(shù)設置的參數(shù)
follow=None, # 是否從response跟進鏈接:不要指定為parse
process_links=None, # 過濾linkextractor列表寸宵,每次獲取列表時都會調(diào)用
process_request=None # 過濾request,每次提取request都會調(diào)用
)
]
classscrapy.contrib.spiders.Rule(link_extractor, callback=None, cb_kwargs=None, follow=None,process_links=None, process_request=None)
link_extractor
是一個 Link Extractor 對象崖面。 其定義了如何從爬取到的頁面提取鏈接。
callback
是一個callable或string(該spider中同名的函數(shù)將會被調(diào)用)梯影。 從link_extractor中每獲取到鏈接時將會調(diào)用該函數(shù)巫员。該回調(diào)函數(shù)接受一個response作為其第一個參數(shù), 并返回一個包含Item
以及(或) Request
對象(或者這兩者的子類)的列表(list)甲棍。
警告
當編寫爬蟲規(guī)則時简识,請避免使用 parse作為回調(diào)函數(shù)。 由于 CrawlSpider 使用 parse方法來實現(xiàn)其邏輯感猛,如果 您覆蓋了 parse方法财异,crawl spider 將會運行失敗。
cb_kwargs 包含傳遞給回調(diào)函數(shù)的參數(shù)(keyword argument)的字典唱遭。
follow 是一個布爾(boolean)值戳寸,指定了根據(jù)該規(guī)則從response提取的鏈接是否需要跟進。 如果callback 為None拷泽, follow 默認設置為 True 疫鹊,否則默認為 False 。
process_links 是一個callable或string(該spider中同名的函數(shù)將會被調(diào)用)司致。 從link_extractor中獲取到鏈接列表時將會調(diào)用該函數(shù)拆吆。該方法主要用來過濾
process_request 是一個callable或string(該spider中同名的函數(shù)將會被調(diào)用)。 該規(guī)則提取到每個request時都會調(diào)用該函數(shù)脂矫。該函數(shù)必須返回一個request或者None枣耀。 (用來過濾request)
CrawlSpider樣例
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
class MySpider(CrawlSpider):
name = 'example.com'
allowed_domains = ['example.com']
start_urls = ['http://www.example.com']
rules = (
# 提取匹配 'category.php' (但不匹配 'subsection.php') 的鏈接并跟進鏈接(沒有callback意味著follow默認為True)
Rule(LinkExtractor(allow=('category\.php', ), deny=('subsection\.php', ))),
# 提取匹配 'item.php' 的鏈接并使用spider的parse_item方法進行分析
Rule(LinkExtractor(allow=('item\.php', )), callback='parse_item'),
)
def parse_item(self, response):
self.log('Hi, this is an item page! %s' % response.url)
item = scrapy.Item()
item['id'] = response.xpath('//td[@id="item_id"]/text()').re(r'ID: (\d+)')
item['name'] = response.xpath('//td[@id="item_name"]/text()').extract()
item['description'] = response.xpath('//td[@id="item_description"]/text()').extract()
return item
該spider將從example.com的首頁開始爬取,獲取category以及item的鏈接并對后者使用 parse_item
方法庭再。 當item獲得返回(response)時捞奕,將使用XPath處理HTML并生成一些數(shù)據(jù)填入 Item
中
4.如何在pycharm中啟動爬蟲程序
在項目下創(chuàng)建start.py文件
# -*- coding:utf-8 -*-
from scrapy import cmdline #引入命令行
cmdline.execute('scrapy crawl dang'.split())
運行文件即可
點擊Edit Configurations
添加python文件
配置完畢后牺堰,點擊ok
點擊運行