Scrapy 在 Python 中使用循环

Scrapy using loops in Python

我想要一个 activity 来抓取网页。资料网部分为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]

但是输出是:

[" '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]})