爬虫之spider样本代码之一

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from scrapy.spider import Spider
from scrapy.selector import Selector
from tutorial.items import DmozItem

            
class DmozSpider(Spider):
    name = "dmoz"
    allowed_domains = [“dmoz.org”]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/",
    ]

    def parse(self, response):
        sel = Selector(response)
        sites = sel.xpath('//ul[@class=”directory-url”]/li')
        items = []

        for site in sites:
            item = DmozItem()
            item[‘name’] = site.xpath('a/text()').extract()
            item[‘url’] = site.xpath('a/@href').extract()
            item[‘description’] = site.xpath(‘text()’).re(‘-\s[^\n]*\\r')
            items.append(item)

        return items

Author: bkdwei