如何用scrapy提取不在标签内的文字
2个回答
2016-09-09 · 知道合伙人软件行家
关注
展开全部
代码如下
def parse(self,response):
states = {}
list1 = []
list2 = []
for row in response.xpath("//*[@id='info']/*"):
if row.xpath("span[@class='pl']/text()"):
title = row.xpath("span[@class='pl']/text()").extract()[0].strip()
text = row.xpath("a/text()").extract()[0].strip()
states[title]=text
elif row.xpath("text()"):
list1.append(row.xpath("text()").extract()[0].strip()[:-1])
for row in response.xpath("//*[@id='info']/text()").extract():
if row.strip():
list2.append(row.strip())
for i in range(len(list1)):
states[list1[i]]=list2[i]
for n in states:
print n,states[n]
def parse(self,response):
states = {}
list1 = []
list2 = []
for row in response.xpath("//*[@id='info']/*"):
if row.xpath("span[@class='pl']/text()"):
title = row.xpath("span[@class='pl']/text()").extract()[0].strip()
text = row.xpath("a/text()").extract()[0].strip()
states[title]=text
elif row.xpath("text()"):
list1.append(row.xpath("text()").extract()[0].strip()[:-1])
for row in response.xpath("//*[@id='info']/text()").extract():
if row.strip():
list2.append(row.strip())
for i in range(len(list1)):
states[list1[i]]=list2[i]
for n in states:
print n,states[n]
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询