【发布时间】:2016-01-18 02:19:43
【问题描述】:
我正在使用scrapy来抓取这个页面:
https://en.wikipedia.org/wiki/List_of_shopping_malls_in_the_United_States
链接在:
data = response.xpath('//*[@id="mw-content-text"]/ul[9]/li').extract()
数据在哪里:
[<Selector xpath='//*/li' data=u'<li><a href="/wiki/Ala_Moana_Center" tit'>,
<Selector xpath='//*/li' data=u'<li><a href="/wiki/Kahala_Mall" title="K'>,
<Selector xpath='//*/li' data=u'<li><a href="/wiki/Pearlridge" title="Pe'>,
<Selector xpath='//*/li' data=u'<li><a href="/wiki/Prince_Kuhio_Plaza" t'>,
<Selector xpath='//*/li' data=u'<li><a href="/wiki/Victoria_Ward_Centers'>]
我需要的链接是:
https://en.wikipedia.org + href
例如:
'https://en.wikipedia.org'+'/wiki/Ala_Moana_Center'
为此,我正在使用正则表达式
link = 'https://en.wikipedia.org' + re.findall('href="([^"]+)',str(data[0]))[0]
name = re.findall('href="([^"]+)',str(data[0]))[0].replace('/wiki/','').replace('_',' ')
问题在于,使用这种方法我必须创建一个循环来获取链接,有一种方法可以直接从 scrapy 或至少以更有效的方式创建这些链接?
【问题讨论】: