簡體   English   中英

Scrapy 在 Python 中使用循環

[英]Scrapy using loops in Python

我想要一個 scrapy 一個 web 頁面的活動。 數據 web 的部分是route_data

route_data = ["javascript:mostrarFotografiaHemiciclo( '/wc/htdocs/web/img/diputados/peq/215_14.jpg', '/wc/htdocs/web', 'Batet Lamaña, Meritxell (Presidenta del Congreso de los Diputados)', 'Diputada por Barcelona', 'G.P. Socialista' ,'','');",
 "javascript:mostrarFotografiaHemiciclo( '/wc/htdocs/web/img/diputados/peq/168_14.jpg', '/wc/htdocs/web', 'Rodríguez Gómez de Celis, Alfonso (Vicepresidente Primero)', 'Diputado por Sevilla', 'G.P. Socialista' ,'','');",]

我創建了一個空值的字典。

dictionary_data = {"Nombre":None, "Territorio":None, "Partido":None, "url":None}

我必須在dictionary_data中保存每一行:

url = /wc/htdocs/web/img/diputados/peq/215_14.jpg

Nombre = Batet Lamaña, Meritxell
Territorio = Diputada por Barcelona
Partido = G.P. Socialista

因此,我遍歷route_data

for i in route_data:
    text = i.split(",")
    nombre = text[2:4]
    territorio = text[4]
    partido = text[5]

但是 output 是:

[" 'Batet Lamaña", " Meritxell (Presidenta del Congreso de los Diputados)'"]  'Diputada por Barcelona'  'G.P. Socialista' 
[" 'Rodríguez Gómez de Celis", " Alfonso (Vicepresidente Primero)'"]  'Diputado por Sevilla'  'G.P. Socialista'

如何在字典中正確輸入?

一個簡單的解決方案是:

all_routes = []
for i in route_data:
    text = re.findall("'.+?'", i)
    all_routes.append(
    {"Nombre": re.sub('\(.*?\)', '', text[2]).strip(),
    "Territorio": text[3],
    "Partido": text[-2],
    "Url": text[0]})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM