用scrapy進行網頁抓取
最近用scrapy來進行網頁抓取,對於pythoner來說它用起來非常方便,詳細文件在這裡:http://doc.scrapy.org/en/0.14/index.html
要想利用scrapy來抓取網頁資訊,需要先新建一個工程,scrapy startproject myproject
工程建立好後,會有一個myproject/myproject的子目錄,裡面有item.py(由於你要抓取的東西的定義),pipeline.py(用於處理抓取後的資料,可以儲存資料庫,或是其他),然後是spiders資料夾,可以在裡面編寫爬蟲的指令碼.
這裡以爬取某網站的書籍資訊為例:
item.py如下:
- from scrapy.item import Item, Field
- class BookItem(Item):
- # define the fields for your item here like:
- name = Field()
- publisher = Field()
- publish_date = Field()
- price = Field()
我們要抓取的東西都在上面定義好了,分別是名字,出版商,出版日期,價格,
下面就要寫爬蟲去網戰抓取資訊了,
spiders/book.py如下:
- from urlparse import urljoin
- import simplejson
- from scrapy.http import Request
- from scrapy.contrib.spiders import CrawlSpider, Rule
- from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
- from scrapy.selector import HtmlXPathSelector
- from myproject.items import BookItem
- class BookSpider(CrawlSpider):
- name = 'bookspider'
- allowed_domains = ['test.com']
- start_urls = [
- "http://test_url.com", #這裡寫開始抓取的頁面地址(這裡網址是虛構的,實際使用時請替換)
- ]
- rules = (
- #下面是符合規則的網址,但是不抓取內容,只是提取該頁的連結(這裡網址是虛構的,實際使用時請替換)
- Rule(SgmlLinkExtractor(allow=(r'http://test_url/test?page_index=\d+'))),
- #下面是符合規則的網址,提取內容,(這裡網址是虛構的,實際使用時請替換)
- Rule(SgmlLinkExtractor(allow=(r'http://test_rul/test?product_id=\d+')), callback="parse_item"),
- )
- def parse_item(self, response):
- hxs = HtmlXPathSelector(response)
- item = BookItem()
- item['name'] = hxs.select('//div[@class="h1_title book_head"]/h1/text()').extract()[0]
- item['author'] = hxs.select('//div[@class="book_detailed"]/p[1]/a/text()').extract()
- publisher = hxs.select('//div[@class="book_detailed"]/p[2]/a/text()').extract()
- item['publisher'] = publisher and publisher[0] or ''
- publish_date = hxs.select('//div[@class="book_detailed"]/p[3]/text()').re(u"[\u2e80-\u9fffh]+\uff1a([\d-]+)")
- item['publish_date'] = publish_date and publish_date[0] or ''
- prices = hxs.select('//p[@class="price_m"]/text()').re("(\d*\.*\d*)")
- item['price'] = prices and prices[0] or ''
- return item
然後資訊抓取後,需要儲存,這時就需要寫pipelines.py了(用於scapy是用的twisted,所以具體的資料庫操作可以看twisted的資料,這裡只是簡單介紹如何儲存到資料庫中):
- from scrapy import log
- #from scrapy.core.exceptions import DropItem
- from twisted.enterprise import adbapi
- from scrapy.http import Request
- from scrapy.exceptions import DropItem
- from scrapy.contrib.pipeline.images import ImagesPipeline
- import time
- import MySQLdb
- import MySQLdb.cursors
- class MySQLStorePipeline(object):
- def __init__(self):
- self.dbpool = adbapi.ConnectionPool('MySQLdb',
- db = 'test',
- user = 'user',
- passwd = '******',
- cursorclass = MySQLdb.cursors.DictCursor,
- charset = 'utf8',
- use_unicode = False
- )
- def process_item(self, item, spider):
- query = self.dbpool.runInteraction(self._conditional_insert, item)
- query.addErrback(self.handle_error)
- return item
- def _conditional_insert(self, tx, item):
- if item.get('name'):
- tx.execute(\
- "insert into book (name, publisher, publish_date, price ) \
- values (%s, %s, %s, %s)",
- (item['name'], item['publisher'], item['publish_date'],
- item['price'])
- )
完成之後在setting.py中新增該pipeline:
- ITEM_PIPELINES = ['myproject.pipelines.MySQLStorePipeline']
最後執行scrapy crawl bookspider就開始抓取了
相關文章
- scrapy抓取ajax請求的網頁網頁
- 為什麼需要用代理進行網頁抓取?網頁
- 藉助代理IP進行網頁抓取的終極指南網頁
- 用HTML進行網頁佈局HTML網頁
- 使用代理進行抓取網頁的主要原因是什麼?網頁
- 用Scrapy抓取豆瓣小組資料(一)
- 用Scrapy抓取豆瓣小組資料(二)
- 用Scrapy抓取豆瓣小組資料(三)
- 使用Scrapy抓取資料
- 如何抓取網頁資訊?網頁
- python抓取網頁Python網頁
- Perl 6 網頁抓取網頁
- wget 網頁爬蟲,網頁抓取工具wget網頁爬蟲
- CURL抓取網頁內容並用正則提取。網頁
- 爬取網頁後的抓取資料_3種抓取網頁資料方法網頁
- 網頁資料抓取之噹噹網網頁
- Scrapy爬蟲:實習僧網最新招聘資訊抓取爬蟲
- scrapy實戰之定向抓取某網店商品資料
- Scrapy框架抓取安居客房源資訊框架
- scrapy 爬電影 抓取資料
- 批量抓取網頁pdf檔案網頁
- 使用代理抓取網頁的原因網頁
- 抓取網頁中的原始碼.網頁原始碼
- 從網頁上抓取資料網頁
- 利用WebClient進行資料抓取Webclient
- 使用python進行web抓取PythonWeb
- 爬蟲進階——動態網頁Ajax資料抓取(簡易版)爬蟲網頁
- Java抓取網頁資料(原網頁+Javascript返回資料)網頁JavaScript
- 爬蟲抓取網頁資料原理爬蟲網頁
- C#抓取網頁HTML內容C#網頁HTML
- java抓取網頁的郵件地址Java網頁
- Python多執行緒抓取Google搜尋連結網頁Python執行緒Go網頁
- 網路爬蟲如何獲取IP進行資料抓取爬蟲
- 如何進行網路抓取?
- python:利用asyncio進行快速抓取Python
- 爬網入門:JAVA抓取網站網頁內容Java網站網頁
- NodeJS使用PhantomJs抓取網頁NodeJS網頁
- IP地址在網頁抓取中有何作用網頁