我一直在學習如何使用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網站取得的代碼。我在做什麼錯誤或誤解?
@buffer:基於元數據,它看起來像鏈接被提取,但沒有傳遞給parse_item。 我的規則是這樣的 \t規則=( \t \t規則(SgmlLinkExtractor(),請按照=真,回調= 「parse_item」, \t \t), \t) – ProgrammingAnt