NameError:名称未定义Scrapy

时间:2018-12-04 04:24:43

标签: python scrapy

我正在使用scrapy 1.5创建Web爬虫,并且收到一个错误,提示未定义row_list。我认为这是一个全球性问题,但不确定要解决此问题的正确方法是什么。之所以保留这样的代码,是因为我需要连续包含所有详细信息的子列表。因此,我可以将列表中的第一个元素用作键,并将第一个元素之后的所有其他内容连接到存储在第二个位置的1个元素中。我必须产生一个字典,并需要这种格式。任何帮助都会很棒。

def forklift_detail_parse(self, response):
    final_dict = {}
    # get the table with html tr and td
    table_list = response.css('div.block-wrapp.full-title.product-info > span > div > table > tr').extract()

    for rows in table_list:
        row_list = Selector(text=rows).css(
            'td::text, td > span::text, #pc-price > span:nth-child(1)::text, tr > td > div::text').extract()
        if len(row_list) <= 1:
            del row_list
        if len(row_list[1:]) > 1:
            row_list[1] = ''.join(row_list[1:])
            del row_list[2:]

        final_dict.update(row_list)




line 5 NameError: name 'row_list' is not defined

1 个答案:

答案 0 :(得分:0)

问题似乎在于您正在呼叫del row_list,此后,您尝试使用该名称更新final_dict

查看此示例:

>>> x = 1
>>> x
1
>>> del x
>>> x
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-6fcf9dfbd479> in <module>
----> 1 x

NameError: name 'x' is not defined

调用del row_list时,row_list名称将从名称空间中删除,因此您将无法访问它。

有关del语句的更多信息:https://docs.python.org/3/reference/simple_stmts.html#del