Scrapy 爬虫给出 KeyError
Scrapy crawler gives out KeyError
我正在尝试使用 Scrapy 从英国网站上抓取药物信息,但我得到 "KeyError: 'Item does not support field: title'"。我不知道这里有什么问题。
我尝试使用 scrapy.Spider class 和 parse_item 函数进行抓取。 x 路径似乎工作正常。我一定对 Rule / LinkExtractor 对象有问题?
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class EMCSpider(CrawlSpider):
name = 'emccrawler'
allowed_domains = ['medicines.org.uk']
start_urls = ['https://www.medicines.org.uk/emc/browse-medicines/']
rules = (
Rule(LinkExtractor(restrict_xpaths="//ul[@class='browse']/li/a"),
callback= 'parse_item', follow=True),
Rule(LinkExtractor(restrict_xpaths="//a[@class='search-paging-
next']"), callback= 'parse_item', follow=True),
Rule(LinkExtractor(restrict_xpaths="//div[@class='col-sm-9']/h2/a"), callback= 'parse_item', follow=True),
)
def parse_item(self, response):
yield {
'title': response.xpath("//div[@class='col-md-12 title']/h1/text()").get(),
'company': response.xpath("//h2[@class='product']/a/text()").get(),
'ingredient': response.xpath("//div[@class='col-xs-12 col-sm-6']/ul/li/text()").get(),
'prescription': response.xpath("//div[@class='col-xs-12 col-sm-6']/p/text()").get(),
}
--
对不起大家,我的项目设置似乎有问题。只需创建一个新项目并复制爬虫就可以为我工作。
感谢@tomjn 和大家帮我解决。
我正在尝试使用 Scrapy 从英国网站上抓取药物信息,但我得到 "KeyError: 'Item does not support field: title'"。我不知道这里有什么问题。
我尝试使用 scrapy.Spider class 和 parse_item 函数进行抓取。 x 路径似乎工作正常。我一定对 Rule / LinkExtractor 对象有问题?
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class EMCSpider(CrawlSpider):
name = 'emccrawler'
allowed_domains = ['medicines.org.uk']
start_urls = ['https://www.medicines.org.uk/emc/browse-medicines/']
rules = (
Rule(LinkExtractor(restrict_xpaths="//ul[@class='browse']/li/a"),
callback= 'parse_item', follow=True),
Rule(LinkExtractor(restrict_xpaths="//a[@class='search-paging-
next']"), callback= 'parse_item', follow=True),
Rule(LinkExtractor(restrict_xpaths="//div[@class='col-sm-9']/h2/a"), callback= 'parse_item', follow=True),
)
def parse_item(self, response):
yield {
'title': response.xpath("//div[@class='col-md-12 title']/h1/text()").get(),
'company': response.xpath("//h2[@class='product']/a/text()").get(),
'ingredient': response.xpath("//div[@class='col-xs-12 col-sm-6']/ul/li/text()").get(),
'prescription': response.xpath("//div[@class='col-xs-12 col-sm-6']/p/text()").get(),
}
--
对不起大家,我的项目设置似乎有问题。只需创建一个新项目并复制爬虫就可以为我工作。 感谢@tomjn 和大家帮我解决。