从刮取的数据创建嵌套字典(刮取Python)

2024-06-24 11:47:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我不太确定是否需要使用从网站上收集的数据直接生成词典,或者是否最好先创建一个列表,但我就是这么做的(如果可能,我不想使用熊猫):

使用scrapy从this website中刮取了一个货币值表,并创建了以下列表:

# the value order is "Currency_name", BRL, USD, EUR, GBP, JPY, CHF, CAD, AUD
[" BRL", "1", "0,1800", "0,1517", "0,1361", "19,0340", "0,1633", "0,2372", "0,2501",
" USD", "5,5571", "1", "0,8428", "0,7564", "105,78", "0,9075", "1,3181", "1,3897",
" EUR", "6,5938", "1,1866", "1", "0,8977", "125,49", "1,0769", "1,5640", "1,6488",
" GBP", "7,3460", "1,3219", "1,1140", "1", "139,82", "1,1997", "1,7424", "1,8370",
" JPY", "0,05254", "0,0095", "0,0080", "0,00715", "1", "0,0086", "0,01246", "0,01314",
" CHF", "6,1236", "1,1019", "0,9286", "0,8335", "116,53", "1", "1,4525", "1,5312",
" CAD", "4,2160", "0,7587", "0,6394", "0,5739", "80,25", "0,6886", "1", "1,0543",
" AUD", "3,9989", "0,7196", "0,6065", "0,5444", "76,12", "0,6532", "0,9485", "1"]

到目前为止我的代码:

import scrapy

# inside scrapy.Spider class
def parse(self, response):
    currency_table = response.css("#exchange_rates_1 > tbody > tr > td::text").extract()
    item_list = []
    for data in currency_table:
        items = data.strip("\t").strip("\n").strip("\t") # removing nonsense
        if items.startswith("\xa0"): # country flag code
            item_list.append(items.strip("\xa0"))
        elif items != "":
            item_list.append(items)

我想要的输出是这样的:

currency_dict = {
            "BRL": {
                "Real": 1,
                "Dollar": value,
                "Euro": value,
                "Pound": value,
                "Yen": value,
                "Swiss Frank": value,
                "Canadian Dollar": value,
                "Australian Dollar": value,
            },
            "USD": {
                "Real": value,
                "Dollar": 1,
                "Euro": value,
                ...
            },
            # continues for all currencies
        }

Tags: 列表valueitemseuritemcurrencylistscrapy
1条回答
网友
1楼 · 发布于 2024-06-24 11:47:03

像这样的方法应该会奏效:

import pprint

# from https://stackoverflow.com/a/312464/
def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

cur_lst = ["Real", "Dollar", "Euro", "Pound", "Yen", "Swiss Frank", "Canadian Dollar", "Australian Dollar"]

data = [" BRL", "1", "0,1800", "0,1517", "0,1361", "19,0340", "0,1633", "0,2372", "0,2501",
        " USD", "5,5571", "1", "0,8428", "0,7564", "105,78", "0,9075", "1,3181", "1,3897",
        " EUR", "6,5938", "1,1866", "1", "0,8977", "125,49", "1,0769", "1,5640", "1,6488",
        " GBP", "7,3460", "1,3219", "1,1140", "1", "139,82", "1,1997", "1,7424", "1,8370",
        " JPY", "0,05254", "0,0095", "0,0080", "0,00715", "1", "0,0086", "0,01246", "0,01314",
        " CHF", "6,1236", "1,1019", "0,9286", "0,8335", "116,53", "1", "1,4525", "1,5312",
        " CAD", "4,2160", "0,7587", "0,6394", "0,5739", "80,25", "0,6886", "1", "1,0543",
        " AUD", "3,9989", "0,7196", "0,6065", "0,5444", "76,12", "0,6532", "0,9485", "1"]

currency_dict = {}
for cur_name, *cur_data in chunks(data, 9):
    data_obj = {}
    for name, value in zip(cur_lst, cur_data):
        # Python doesn't like a comma used as a decimal point
        data_obj[name] = float(value.replace(",","."))

    currency_dict[cur_name.strip()] = data_obj
    
pprint.pprint(currency_dict)

输出:

{'AUD': {'Australian Dollar': 1.0,
         'Canadian Dollar': 0.9485,
         'Dollar': 0.7196,
         'Euro': 0.6065,
         'Pound': 0.5444,
         'Real': 3.9989,
         'Swiss Frank': 0.6532,
         'Yen': 76.12},
 'BRL': {'Australian Dollar': 0.2501,
         'Canadian Dollar': 0.2372,
         'Dollar': 0.18,
         'Euro': 0.1517,
         'Pound': 0.1361,
         'Real': 1.0,
         'Swiss Frank': 0.1633,
         'Yen': 19.034},
 'CAD': {'Australian Dollar': 1.0543,
         'Canadian Dollar': 1.0,
         'Dollar': 0.7587,
         'Euro': 0.6394,
         'Pound': 0.5739,
         'Real': 4.216,
         'Swiss Frank': 0.6886,
         'Yen': 80.25},
 'CHF': {'Australian Dollar': 1.5312,
         'Canadian Dollar': 1.4525,
         'Dollar': 1.1019,
         'Euro': 0.9286,
         'Pound': 0.8335,
         'Real': 6.1236,
         'Swiss Frank': 1.0,
         'Yen': 116.53},
 'EUR': {'Australian Dollar': 1.6488,
         'Canadian Dollar': 1.564,
         'Dollar': 1.1866,
         'Euro': 1.0,
         'Pound': 0.8977,
         'Real': 6.5938,
         'Swiss Frank': 1.0769,
         'Yen': 125.49},
 'GBP': {'Australian Dollar': 1.837,
         'Canadian Dollar': 1.7424,
         'Dollar': 1.3219,
         'Euro': 1.114,
         'Pound': 1.0,
         'Real': 7.346,
         'Swiss Frank': 1.1997,
         'Yen': 139.82},
 'JPY': {'Australian Dollar': 0.01314,
         'Canadian Dollar': 0.01246,
         'Dollar': 0.0095,
         'Euro': 0.008,
         'Pound': 0.00715,
         'Real': 0.05254,
         'Swiss Frank': 0.0086,
         'Yen': 1.0},
 'USD': {'Australian Dollar': 1.3897,
         'Canadian Dollar': 1.3181,
         'Dollar': 1.0,
         'Euro': 0.8428,
         'Pound': 0.7564,
         'Real': 5.5571,
         'Swiss Frank': 0.9075,
         'Yen': 105.78}}

相关问题 更多 >