我正在尝试运行Scrapy蜘蛛并将其全部转储到json文件中。这是我的代码:
import scrapy
import re
class MissleItem(scrapy.Item):
missle_name = scrapy.Field()
missle_type = scrapy.Field()
missle_origin = scrapy.Field()
missle_range = scrapy.Field()
missle_comments = scrapy.Field()
class missleSpider(scrapy.Spider):
name = 'missle_list'
allowed_domains = ['en.wikipedia.org']
start_urls = ['https://en.wikipedia.org/wiki/...']
def parse(self, response):
table = response.xpath('///div/table[2]/tbody')
rows = table.xpath('//tr')
row = rows[2]
row.xpath('td//text()')[0].extract()
for row in response.xpath('// \
[@class="wikitable"]//tbody//tr'):
name = {
'Missle' : row.xpath('td[1]//text()').extract_first(),
'Type': row.xpath('td[2]//text()').extract_first(),
'Origin' :
row.xpath('td[3]/a//text()').extract_first(),
'Range':
row.xpath('td[4]//text()').replace(u'\ ', u'
').extract_first(),
'Comments' :
row.xpath('td[5]//text()').extract_first()}
yield MissleItem(missle_name=name['Missle'],
missle_type=name['Type'],
missle_origin=name['Origin'],
missle_range=name['Range'],
missle_comments=name['Comments'])
运行前面的代码时,我得到: AttributeError:“ SelectorList”对象没有属性“ replace”
我的问题是,如何返回没有“ nbsp;”的范围列额外的输出?我尝试过:
'Range': row.xpath('td[4]//text()').strip().extract_first()
但是后来我得到了:
AttributeError: 'SelectorList' object has no attribute 'strip'
任何帮助将不胜感激
答案 0 :(得分:1)
row.xpath('td[4]//text()').replace(u'\ ', u'').extract_first(),
尝试将extract_first()
放在replace属性之前