关于scrapy入门级代码tutorial示例代码无法获得item的原因

        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",  

        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"  

这两个网站结构已经改变了,不再是ul/li的格式


网上流传的dmoz_spider.py代码为:

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['title'] = site.xpath('a/text()').extract()  
            item['link'] = site.xpath('a/@href').extract()  
            item['desc'] = site.xpath('text()').extract()  
            items.append(item)  
        return items


改变后dmoz_spider.py的代码为,items.py文件不变:

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('//div[@class="title-and-desc"]') items = [] for site in sites: item = DmozItem() item['title'] = site.xpath('a/div[@class="site-title"]/text()').extract() item['link']=site.xpath('a/@href').extract() item['desc']=site.xpath('div/text()').extract() items.append(item) return items
 
  


编辑日期:2016年11月5日02:14:05

你可能感兴趣的:(关于scrapy入门级代码tutorial示例代码无法获得item的原因)