【百度雲搜索,搜各種資料:http://www.lqkweb.com】
【搜網盤,搜各種資料:http://www.swpan.cn】
xpath表達式
//x 表示向下查找n層指定標籤,如://div 表示查找所有div標籤
/x 表示向下查找一層指定的標籤
/@x 表示查找指定屬性的值,可以連綴如:@id @src
[@屬性名稱="屬性值"]表示查找指定屬性等於指定值的標籤,可以連綴 ,如查找class名稱等於指定名稱的標籤
/text() 獲取標籤文本類容
[x] 通過索引獲取集合裏的指定一個元素
1、將xpath表達式過濾出來的結果進行正則匹配,用正則取最終內容
最後.re('正則')
xpath('//div[@class="showlist"]/li//img')[0].re('alt="(\w+)')
2、在選擇器規則裏應用正則進行過濾
[re:正則規則]
xpath('//div[re:test(@class, "showlist")]').extract()
實戰使用Scrapy獲取一個電商網站的、商品標題、商品鏈接、和評論數
分析源碼
第一步、編寫items.py容器文件
我們已經知道了我們要獲取的是、商品標題、商品鏈接、和評論數
在items.py創建容器接收爬蟲獲取到的數據
設置爬蟲獲取到的信息容器類,必須繼承scrapy.Item類
scrapy.Field()方法,定義變量用scrapy.Field()方法接收爬蟲指定字段的信息
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html
import scrapy
#items.py,文件是專門用於,接收爬蟲獲取到的數據信息的,就相當於是容器文件
class AdcItem(scrapy.Item): #設置爬蟲獲取到的信息容器類
# define the fields for your item here like:
# name = scrapy.Field()
title = scrapy.Field() #接收爬蟲獲取到的title信息
link = scrapy.Field() #接收爬蟲獲取到的連接信息
comment = scrapy.Field() #接收爬蟲獲取到的商品評論數
第二步、編寫pach.py爬蟲文件
定義爬蟲類,必須繼承scrapy.Spider
name設置爬蟲名稱
allowed_domains設置爬取域名
start_urls設置爬取網址
parse(response)爬蟲回調函數,接收response,response裏是獲取到的html數據對象
xpath()過濾器,參數是xpath表達式
extract()獲取html數據對象裏的數據
yield item 接收了數據的容器對象,返回給pipelies.py
# -*- coding: utf-8 -*-
import scrapy
from adc.items import AdcItem #導入items.py裏的AdcItem類,容器類
class PachSpider(scrapy.Spider): #定義爬蟲類,必須繼承scrapy.Spider
name = 'pach' #設置爬蟲名稱
allowed_domains = ['search.dangdang.com'] #爬取域名
start_urls = ['http://category.dangdang.com/pg1-cid4008149.html'] #爬取網址
def parse(self, response): #parse回調函數
item = AdcItem() #實例化容器對象
item['title'] = response.xpath('//p[@class="name"]/a/text()').extract() #表達式過濾獲取到數據賦值給,容器類裏的title變量
# print(rqi['title'])
item['link'] = response.xpath('//p[@class="name"]/a/@href').extract() #表達式過濾獲取到數據賦值給,容器類裏的link變量
# print(rqi['link'])
item['comment'] = response.xpath('//p[@class="star"]//a/text()').extract() #表達式過濾獲取到數據賦值給,容器類裏的comment變量
# print(rqi['comment'])
yield item #接收了數據的容器對象,返回給pipelies.py
robots協議
注意:如果獲取的網站在robots.txt文件裏設置了,禁止爬蟲爬取協議,那麼將無法爬取,因爲scrapy默認是遵守這個robots這個國際協議的,如果想不遵守這個協議,需要在settings.py設置
到settings.py文件裏找到ROBOTSTXT_OBEY變量,這個變量等於False不遵守robots協議,等於True遵守robots協議
# Obey robots.txt rules
ROBOTSTXT_OBEY = False #不遵循robots協議
第三步、編寫pipelines.py數據處理文件
如果需要pipelines.py裏的數據處理類能工作,需在settings.py設置文件裏的ITEM_PIPELINES變量裏註冊數據處理類
# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'adc.pipelines.AdcPipeline': 300, #註冊adc.pipelines.AdcPipeline類,後面一個數字參數表示執行等級,數值越大越先執行
}
註冊後pipelines.py裏的數據處理類就能工作
定義數據處理類,必須繼承object
process_item(item)爲數據處理函數,接收一個item,item裏就是爬蟲最後yield item 來的數據對象
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
class AdcPipeline(object): #定義數據處理類,必須繼承object
def process_item(self, item, spider): #process_item(item)爲數據處理函數,接收一個item,item裏就是爬蟲最後yield item 來的數據對象
for i in range(0,len(item['title'])): #可以通過item['容器名稱']來獲取對應的數據列表
title = item['title'][i]
print(title)
link = item['link'][i]
print(link)
comment = item['comment'][i]
print(comment)
return item
最後執行
執行爬蟲文件,scrapy crawl pach --nolog
可以看到我們需要的數據已經拿到了
【轉載自:http://www.lqkweb.com】