2011-07-11 1 views
2

私は最初にPythonで最小限の経験を持っていましたが、私はscrapyの使い方を学んできました。私は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!!!!!!!!" 

しかし、私は、実行時に、このprint文が表示されませんから、私はこの機能が到達されていないことを恐れています。これは私が公式の治療サイトから直接取ったコードです。私は間違ったことや誤解をしていますか?

+0

@buffer:メタデータに基づいて、リンクが抽出されたものの、parse_itemに渡されないようです。 マイルールがこの \tルール=のように見えます( \t \tルール(SgmlLinkExtractor()、) \t \t、= Trueの場合、コールバック= "parse_item" に続く、 \t) – ProgrammingAnt

答えて

0

あなたが知っているスパイダーを作ってみて、プリントステートメントがあなたが持っている場所で何かをするかどうかを調べるかもしれません。私は長いこと前に同じことをやろうとしたことを覚えていると思いますし、コードが実行されても表示されません。

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

example.comには、カテゴリやアイテムのリンクがありません。これは、スクレイプされたサイトのURLの例です。

これは、ドキュメント内の動作しない例です。

+0

非作業例は本当に悪い例です。 – kev

関連する問題