我一直在学习如何使用scrapy,尽管我一开始对python几乎没有经验。我开始学习如何使用BaseSpider进行抓取。现在我正在尝试抓取网站,但我遇到了一个真正让我困惑的问题。以下是来自http://doc.scrapy.org/topics/spiders.html官方站点的示例代码。
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语句,所以我担心这个函数不会被使用。这是我直接从官方scrapy站点获取的代码。我做错了什么或误解了什么?
发布于 2011-07-20 18:55:17
start_urls = ['http://www.example.com']example.com没有任何类别或项目的链接。这只是一个抓取网站URL的例子。
这是文档中的一个非工作示例。
发布于 2011-07-12 03:10:46
您可以尝试创建一个已知有效的爬行器,并查看print语句是否能在拥有它们的地方执行任何操作。我想我记得很久以前我曾尝试过做同样的事情,但即使代码被执行了,它们也不会出现。
https://stackoverflow.com/questions/6653535
复制相似问题