gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:07:04 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com