顯示具有 crawling 標籤的文章。 顯示所有文章
顯示具有 crawling 標籤的文章。 顯示所有文章

如何在 scrapy shell 裡正確顯示中文

2012年10月4日 星期四
0 意見
photo credit: molechaser

Scrapy shell 提供了很多互動方式,便於檢查網頁結構和設計的擷取規則究竟適當與否,是個非常方便的功能。然而要怎麼樣才能在 shell 裡看見中文字呢?答案很簡單 ─ 使用 print (或許加上 encode)

// 進入 scrapy shell
scrapy shell

// 以 yahoo 電影的排行榜網頁為例

>>> fetch("http://tw.movie.yahoo.com/chart.html")

2012-10-04 17:36:09+0800 [default] INFO: Spider opened
2012-10-04 17:36:09+0800 [default] DEBUG: Crawled (200) <GET http://tw.movie.yahoo.com/chart.html> (referer: None)
[s] Available Scrapy objects:
[s]   hxs        <HtmlXPathSelector xpath=None data=u'<html lang="zh-tw"><head><title>\u53f0\u5317\u7968\u623f\u699c - '>
[s]   item       {}
[s]   request    <GET http://tw.movie.yahoo.com/chart.html>
[s]   response   <200 http://tw.movie.yahoo.com/chart.html>
[s]   settings   <CrawlerSettings module=None>
[s]   spider     <BaseSpider 'default' at 0x1a72f90>
[s] Useful shortcuts:
[s]   shelp()           Shell help (print this help)
[s]   fetch(req_or_url) Fetch request (or URL) and update local objects
[s]   view(response)    View response in a browser


// 選取第一個 option 元素,抽出文字
>>> desc = hxs.select('//option/text()').extract()[0]

// 原始 unicode 格式
>>> desc
u'\u96fb\u5f71\u6642\u523b'

// 正確地顯示中文
>>> print desc
電影時刻

>>> print desc.encode('utf-8')
電影時刻

閱讀更多 »

crawler 軟體清單 & scrapy 的替代物

2012年9月29日 星期六
0 意見
photo credit: Ian Sane

Scrapy 是什麼?來看看官方的定義:

Scrapy is a fast high-level screen scraping and web crawling framework, used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to monitoring and automated testing.

哇嗚,它可以用來扒取網站,擷取網頁上結構化的資料。100% python,可以在 Linux, Windows, Mac 及 BSD 上運行,而且,有很詳盡的說明文件 ...嗯聽起來挺不賴的嘛。

然而我還是想知道有哪些可用的取代軟體,這時候有個聲音傳來了:

If you're looking for a python based crawler, Scrapy is probably your best bet.
─ Eric Wu

所以意思是 scrapy 已經非常好了是嗎?無論如何,Eric Wu 還真是個好心人,他在 Quora 留下了非常有用的爬蟲 (crawler) 清單,記錄用各式各樣語言寫成的爬蟲軟體。

Java
    Nutch => http://nutch.apache.org/
    Heritrix => https://webarchive.jira.com/wiki/display/Heritrix/Heritrix...
    WebSPHINX => http://www.cs.cmu.edu/~rcm/websphinx/

Python
    Scrapy => http://scrapy.org/
    Scrape.py => http://zesty.ca/scrape/
    HarvestMan => http://harvestmanontheweb.com/
    Mechanized (ported from the perl version) => http://wwwsearch.sourceforge.net/mechanize/

Ruby
    scRUBYt => https://github.com/scrubber/scrubyt
    Anemone => http://anemone.rubyforge.org/

Ruby: Not Really Crawlers but can be used like one
    hpricot => http://hpricot.com/
    Nokogiri => http://nokogiri.org/

PHP
    Snoopy => http://sourceforge.net/projects/snoopy/
    PHPCrawl => http://phpcrawl.cuab.de/

Erlang
    eBot => https://github.com/matteoredaelli/ebot


這個清單可以無窮地長下去,然而這是我很不樂見的 XD。因為 python 對我來說是個蠻美的語言,所以我會比較偏好先試用 python based 的軟體。你用過哪些爬蟲軟體呢?如果有推薦的爬蟲軟體,歡迎告訴筆者囉 :)
閱讀更多 »

在使用 Scrapy 之前: Scrapy 安裝方法

0 意見


在使用 Scrapy 之前,有一些套件必須先安裝好:

  1. Python 2.6 或 2.7
  2. OpenSSL (除了 Windows 之外的作業系統都有配備)
  3. pip easy_install python 套件管理


▄▄▄

Scrapy 官方的安裝指南在開頭處寫說用 pip 或 easy_install 皆可,然而我卻 pip 失敗了,因此我改成用 easy_install:

※ 筆者的使用環境是 Virtual Machine 上的 ubuntu 12.04 32-bit 作業系統

// 先安裝 easy_install pytohn 模組
sudo apt-get install python-setuptools
// 透過 easy_install 安裝 scrapy
sudo easy_install scrapy

好了,安裝完後,先查看一下版本
$scrapy version
Scrapy 0.14.4


※ (2012.12.04)
筆者之後在另外一台電腦安裝 Scrapy 時,遇到沒有 python.h 而導致安裝失敗的問題。網路上的朋友很好心地提供了非常簡單的解決辦法,安裝 python 的開發套件。只要在終端機前下指令:

sudo apt-get install python-dev

▄▄▄

恭喜夫人,賀喜老爺,scrapy 已經安裝好了。可以參考底下連結,邁向下一個關卡。


※ 使用 easy_install 的話,安裝好的scrapy 應是位於  /usr/local/lib/python2.6/dist-packages/Scrapy-0.14.4-py2.7.egg/
※ 若想要移除 scrapy,可以 rm -rf 該資料夾,或者使用 pip uninstall
閱讀更多 »
 

Categories

 

© 2010 取火之路, Design by DzigNine
In collaboration with Breaking News, Trucks, SUV