python - 属性错误 : 'SelectorList' object has no attribute 'replace'

标签 python scrapy

我正在尝试运行一个 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'

任何帮助将不胜感激

最佳答案

row.xpath('td[4]//text()').replace(u'\ ', u'').extract_first(),

尝试将 extract_first() 放在替换属性之前

关于python - 属性错误 : 'SelectorList' object has no attribute 'replace' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52896618/

相关文章:

python - 如何计算部分曲线下面积 (AUC)

Python 比较整数并使用 if

python - 带有中文字符的 JSON 的 scrapy 管道

python - Scrapy 循环 - xpath 选择器转义它所应用的对象并返回所有记录?

python - Scrapy:收集重试消息

Python 日志记录 : FileHandler when in mode 'w' not working but in mode 'a' works fine

Python 请求修改现有 cookie 值的正确方法是什么?

Python Scrapy,如何为项目定义管道?

python - 使用 scrapy 版本 0.22.1 进行多页抓取 - "cannot import name CrawlSpider"错误是什么意思?

python - PyEZ: 'EntryPoint' 对象没有属性 'resolve'