2011-07-11 45 views
2

我一直在學習如何使用scrapy,儘管我在python開始時只有很少的經驗。我開始學習如何使用BaseSpider進行刮擦。現在我正在嘗試抓取網站,但遇到了令我困惑的問題。以下是官方網站http://doc.scrapy.org/topics/spiders.html的示例代碼。Python CrawlSpider

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from scrapy.item import Item 

class MySpider(CrawlSpider): 
    name = 'example.com' 
    allowed_domains = ['example.com'] 
    start_urls = ['http://www.example.com'] 

    rules = (
     # Extract links matching 'category.php' (but not matching 'subsection.php') 
     # and follow links from them (since no callback means follow=True by  default). 
     Rule(SgmlLinkExtractor(allow=('category\.php',), deny=('subsection\.php', ))), 

     # Extract links matching 'item.php' and parse them with the spider's method  parse_item 
     Rule(SgmlLinkExtractor(allow=('item\.php',)), callback='parse_item'),) 

    def parse_item(self, response): 
     print "WHY WONT YOU WORK!!!!!!!!" 
     self.log('Hi, this is an item page! %s' % response.url) 

     hxs = HtmlXPathSelector(response) 
     item = TestItem() 
     item['id'] = hxs.select('//td[@id="item_id"]/text()').re(r'ID: (\d+)') 
     item['name'] = hxs.select('//td[@id="item_name"]/text()').extract() 
     item['description'] =  hxs.select('//td[@id="item_description"]/text()').extract() 
     return item 

我做出的唯一改變是語句:

print "WHY WONT YOU WORK!!!!!!!!" 

但因爲我不能在運行時看到這個打印語句,我擔心是沒有達到這個功能。這是我直接從官方scrapy網站取得的代碼。我在做什麼錯誤或誤解?

+0

@buffer:基於元數據,它看起來像鏈接被提取,但沒有傳遞給parse_item。 我的規則是這樣的 \t規則=( \t \t規則(SgmlLinkExtractor(),請按照=真,回調= 「parse_item」, \t \t), \t) – ProgrammingAnt

回答

0

你可能會嘗試製作一個你知道有效的蜘蛛,看看打印語句是否在你有它們的地方做了任何事情。我想我很久以前就想過做同樣的事情,即使代碼被執行,也不會出現。

1
start_urls = ['http://www.example.com'] 

example.com沒有任何類別或項目的鏈接。這只是一個刮臉網站URL的例子。

這是文檔中的非工作示例。

+0

非工作的例子是非常糟糕的例子。 – kev