问题
I'm trying to fill my Items with parsed data and I'm getting error:
item = items()
NameError: name 'items' is not defined**
When I run scrapy crawl usa_florida_scrapper
Here's my spider's code:
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
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
回答1:
You are trying to instantiate something called items()
that doesn't exist anywhere in your code.
def parse(self, response):
item = items()
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
here is a placeholder
回答2:
First import your item class at top, replace your_project_name with your scrapy project name.
from your_project_name.items import KariedanielItem
Next replace this line item = items()
with item = FeboughtItem()
in parse
function.
your code will be like this,
import scrapy
import re
from your_project_name.items import KariedanielItem
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 = KariedanielItem()
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
来源:https://stackoverflow.com/questions/64296656/scrapy-nameerror-name-items-is-not-defined