Scrapy - NameError:未定义名称“项目" [英] Scrapy - NameError: name 'items' is not defined

查看:104
本文介绍了Scrapy - NameError:未定义名称“项目"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用解析的数据填充我的项目,但出现错误:

I'm trying to fill my Items with parsed data and I'm getting error:

item = items()

NameError: name 'items' is not defined**

当我运行 scrapy crawl usa_florida_scrapper

这是我的蜘蛛代码:

import scrapy
import re

class UsaFloridaScrapperSpider(scrapy.Spider):
    name            = 'usa_florida_scrapper'
    start_urls      = ['https://www.txlottery.org/export/sites/lottery/Games/index.html']    

    def parse(self, response):    
        item = items()      
        
        print('++++++ Latest Results for Powerball  ++++++++++')
        power_ball_html = (response.xpath("/html/body/div[1]/div[3]/div/div[1]/div[3]/div[2]/ol").extract_first())
        power_balls=(",".join(re.findall(r'<span>(.+)</span>',power_ball_html)))
        power_ball_special=(response.xpath("/html/body/div[1]/div[3]/div/div[1]/div[3]/div[2]/ol/li[6]/span[contains(@class, 'powerball')]/text()").get())        
        power_ball_jackpot = response.xpath('/html/body/div[1]/div[3]/div/div[1]/div[3]/div[1]/h1/text()').get()
        power_ball_multiplier = response.xpath('/html/body/div[1]/div[3]/div/div[1]/div[3]/div[2]/div[2]/h3/span/text()').get()
        
        item['LotteryKey']= '227'
        item['Date']= '2020-10-10'
        item['Balls']= power_balls
        item['SpecialBalls']= power_ball_special
        item['Multiplier']= power_ball_multiplier
        item['JackpotValue']= power_ball_jackpot
        
        
        yield item

这是我的商品代码 items.py:

Here's my items code items.py:

import scrapy

class KariedanielItem(scrapy.Item):
    # define the fields for your item here like:
    LotteryKey = scrapy.Field()
    Date = scrapy.Field()
    Balls = scrapy.Field()
    SpecialBalls = scrapy.Field()
    Multiplier = scrapy.Field()
    JackpotValue = scrapy.Field()
    pass

推荐答案

您正在尝试实例化一个名为 items() 的东西,它在您的代码中的任何地方都不存在.

You are trying to instantiate something called items() that doesn't exist anywhere in your code.

def parse(self, response):    
    item = items()      

如果您将 Item 类定义为 KariedanielItem,那么这就是您需要实例化的内容.

If you defined your Item class to be KariedanielItem, then that's what you need to instantiate.

def parse(self, response):    
    item = KariedanielItem()   

请记住,您还需要将该类导入到蜘蛛中.

Remember, you will also need to import that class into the spider.

from your_project.items import KariedanielItem

your_project 这里是一个占位符

your_project here is a placeholder

这篇关于Scrapy - NameError:未定义名称“项目"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆