简体   繁体   English

我可以在 python 中为 dict 动态创建密钥吗?

[英]Can i dynamicaly create keys for dict in python?

I am creating a spider in Scrapy.我正在 Scrapy 中创建一个蜘蛛。 And I want to scrape table in this way:我想以这种方式刮桌子:

  • Take every <tr>获取每个<tr>
  • Use <th> as key and <td> as content使用<th>作为键,使用<td>作为内容

The code I came up with is this.我想出的代码是这样的。

def parse(self, response):
        item = {}
        item['code'] = response.xpath('//meta[@itemprop="sku"]/@content').extract_first()
        tables = response.css('.technical-specs')
        for table in tables:
            specs = tables.xpath('tbody/tr')
            for s in specs:
                key = s.xpath('th/text()').extract_first().replace(" ", "_").replace("(", "_").replace(")", "_").replace("/", "").lower()
                value = s.xpath('td/text()').extract_first()
                item[key] = value


        return item

But it is not working.但它不起作用。 Is this posible to achieve?这有可能实现吗?

You need to create a dict instance and then add the items inside the loop.您需要创建一个 dict 实例,然后在循环内添加项目。 Eg:例如:


my_dict = dict() # Can be {} to

for item in items:
  key = item.key
  value = item.value
  my_dict[key] = value

Regards

The, now working, code of parse function is updated in my question details.我的问题详细信息中更新了解析 function 的现在工作代码。 Problem was not in the way loop or dictionary was implemented, but in how I extracted data.问题不在于循环或字典的实现方式,而在于我如何提取数据。 I was using .extract() which makes response unicode and "unscrapable".我正在使用.extract()这使得响应 unicode 和“不可回收”。 Removing.extract was the fix. Remove.extract 是解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM