首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python CrawlSpider

Python CrawlSpider
EN

Stack Overflow用户
提问于 2011-07-12 00:46:13
回答 2查看 1.2K关注 0票数 2

我一直在学习如何使用scrapy,尽管我一开始对python几乎没有经验。我开始学习如何使用BaseSpider进行抓取。现在我正在尝试抓取网站,但我遇到了一个真正让我困惑的问题。以下是来自http://doc.scrapy.org/topics/spiders.html官方站点的示例代码。

代码语言:javascript
复制
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

我所做的唯一更改是语句:

代码语言:javascript
复制
print "WHY WONT YOU WORK!!!!!!!!"

但是由于我在运行时看不到这个print语句,所以我担心这个函数不会被使用。这是我直接从官方scrapy站点获取的代码。我做错了什么或误解了什么?

EN

回答 2

Stack Overflow用户

发布于 2011-07-20 18:55:17

代码语言:javascript
复制
start_urls = ['http://www.example.com']

example.com没有任何类别或项目的链接。这只是一个抓取网站URL的例子。

这是文档中的一个非工作示例。

票数 1
EN

Stack Overflow用户

发布于 2011-07-12 03:10:46

您可以尝试创建一个已知有效的爬行器,并查看print语句是否能在拥有它们的地方执行任何操作。我想我记得很久以前我曾尝试过做同样的事情,但即使代码被执行了,它们也不会出现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6653535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档