【Scrapy】scrapy 抓取csdn內容(轉載)

首先建立project:python

[python] view plain copy 在CODE上查看代碼片派生到個人代碼片
scrapy startproject CSDNBlog

一. items.py編寫 在這裏爲清晰說明,只提取文章名稱和文章網址。web

[python] view plain copy 在CODE上查看代碼片派生到個人代碼片
# -*- coding:utf-8 -*-  
  
from scrapy.item import Item, Field  
  
class CsdnblogItem(Item):  
    """存儲提取信息數據結構"""  
  
    article_name = Field()  
    article_url = Field()

二. pipelines.py編寫json

[python] view plain copy 在CODE上查看代碼片派生到個人代碼片
import json  
import codecs  
  
class CsdnblogPipeline(object):  
  
    def __init__(self):  
        self.file = codecs.open('CSDNBlog_data.json', mode='wb', encoding='utf-8')  
  
    def process_item(self, item, spider):  
        line = json.dumps(dict(item)) + '\n'  
        self.file.write(line.decode("unicode_escape"))  
  
        return item

其中,構造函數中以可寫方式建立並打開存儲文件。在process_item中實現對item處理,包含將獲得的item寫入到json形式的輸出文件中。服務器

三. settings.py編寫 對於setting文件,他做爲配置文件,主要是至執行對spider的配置。一些容易被改變的配置參數能夠放在spider類的編寫中,而幾乎在爬蟲運行過程當中不改變的參數在settings.py中進行配置。cookie

[python] view plain copy 在CODE上查看代碼片派生到個人代碼片
# -*- coding:utf-8 -*-  
  
BOT_NAME = 'CSDNBlog'  
  
SPIDER_MODULES = ['CSDNBlog.spiders']  
NEWSPIDER_MODULE = 'CSDNBlog.spiders'  
  
#禁止cookies,防止被ban  
COOKIES_ENABLED = False  
  
ITEM_PIPELINES = {  
    'CSDNBlog.pipelines.CsdnblogPipeline':300  
}  
  
# Crawl responsibly by identifying yourself (and your website) on the user-agent  
#USER_AGENT = 'CSDNBlog (+http://www.yourdomain.com)'

這裏將COOKIES_ENABLED參數置爲True,使根據cookies判斷訪問的站點不能發現爬蟲軌跡,防止被ban。數據結構

ITEM_PIPELINES類型爲字典,用於設置啓動的pipeline,其中key爲定義的pipeline類,value爲啓動順序,默認0-1000。dom

四. 爬蟲編寫 爬蟲編寫始終是重頭戲。原理是分析網頁獲得「下一篇」的連接,並返回Request對象。進而繼續爬取下一篇文章,直至沒有。scrapy

上碼:ide

[python] view plain copy 在CODE上查看代碼片派生到個人代碼片
#!/usr/bin/python  
# -*- coding:utf-8 -*-  
  
# from scrapy.contrib.spiders import  CrawlSpider,Rule  
  
from scrapy.spider import Spider  
from scrapy.http import Request  
from scrapy.selector import Selector  
from CSDNBlog.items import CsdnblogItem  
  
  
class CSDNBlogSpider(Spider):  
    """爬蟲CSDNBlogSpider"""  
  
    name = "CSDNBlog"  
  
    #減慢爬取速度 爲1s  
    download_delay = 1  
    allowed_domains = ["blog.csdn.net"]  
    start_urls = [  
  
        #第一篇文章地址  
        "http://blog.csdn.net/u012150179/article/details/11749017"  
    ]  
  
    def parse(self, response):  
        sel = Selector(response)  
  
        #items = []  
        #得到文章url和標題  
        item = CsdnblogItem()  
  
        article_url = str(response.url)  
        article_name = sel.xpath('//div[@id="article_details"]/div/h1/span/a/text()').extract()  
  
        item['article_name'] = [n.encode('utf-8') for n in article_name]  
        item['article_url'] = article_url.encode('utf-8')  
  
        yield item  
  
        #得到下一篇文章的url  
        urls = sel.xpath('//li[@class="next_article"]/a/@href').extract()  
        for url in urls:  
            print url  
            url = "http://blog.csdn.net" + url  
            print url  
            yield Request(url, callback=self.parse)

慢慢分析: (1)download_delay參數設置爲1,將下載器下載下一個頁面前的等待時間設置爲1s,也是防止被ban的策略之一。主要是減輕服務器端負載。函數

(2)從response中抽取文章連接與文章題目,編碼爲utf-8。注意yield的使用。

(3)抽取「下一篇」的url,因爲抽取後缺乏http://blog.csdn.NET部分,因此補充。兩個print只爲調試,無實際意義。重點在於

[python] view plain copy 在CODE上查看代碼片派生到個人代碼片
yield Request(url, callback=self.parse)

也就是將新獲取的request返回給引擎,實現繼續循環。也就實現了「自動下一網頁的爬取」。

五. 執行

[python] view plain copy 在CODE上查看代碼片派生到個人代碼片
scrapy crawl CSDNBlog
相關文章
相關標籤/搜索