我仍在学习草率,并且制作了蜘蛛。而且我一直收到此错误“ AttributeError:模块'scrapy'没有属性'field '”
这是我的items.py:
import scrapy
class QuoteItem(scrapy.Item):
text = scrapy.field()
author = scrapy.field()
tags = scrapy.field()
这是我的蜘蛛:
import scrapy
from scrapy.loader import ItemLoader
from testing.items import QuoteItem
class GoodReadspider(scrapy.Spider):
name = "goodreads"
def start_requests(self):
url = 'https://www.goodreads.com/quotes?page=1'
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
for quote in response.selector.xpath("//div[@class='quote']"):
loader = ItemLoader(item = QuoteItem, selector = quote, response = response)
loader.add_xpath('text', ".//div[@class='quoteText']/text()[1]")
loader.add_xpath('author', ".//div[@class='quoteText']/child::a")
loader.add_xpath('tags', ".//div[@class='greyText smallText left']/a")
yield loader.load_item()
next_page=response.selector.xpath("//a[@class='next_page']/@href").extract_first()
if next_page is not None:
next_page_link = response.urljoin(next_page)
yield scrapy.Request(url=next_page_link, callback=self.parse)
感谢您的帮助。
答案 0 :(得分:1)
您应该将field
大写(改为Field
):
import scrapy
class QuoteItem(scrapy.Item):
text = scrapy.Field()
author = scrapy.Field()