我需要在h3级机身/h3之后,但在h3级引擎之前提取/h3之后的所有内容。
我需要的是:
“投入服务:2010年12月-自新开始的总时间:3 580小时”等。
下面是我试过的东西,但它什么也不回。总的来说,我对Scrapy和编程很陌生,所以我希望得到一些帮助。我已经尝试过搜索其他帖子和谷歌在一般情况下没有任何运气。
input =response.xpath(“//div@class=‘大-6单元格response.xpath)”
产出= []
发布于 2020-07-07 17:00:51
您正在使用的代码引用的是另一个没有您提到的文本的类。
input = response.xpath("//div[@class='large-6 cell selectorgadget_rejected']/h3/text()").extract()图片中的类名是large-6 cell selectorgadget_selected,而不是large-6 cell selectorgadget_rejected。
另外,如果您使用.../h3/text(),您将在H3标记中刮取文本。据我所知,您希望文本在H3之后,在<div>之间。所以试着做这样的事情:
input = response.xpath("//div[@class='large-6 cell selectorgadget_selected']/text()").extract()发布于 2020-07-07 18:24:54
要完成@renatodvc的回答,可以添加normalize-space函数来忽略空格节点。
//div[@class='large-6 cell selectorgadget_selected']/text()[normalize-space()]或者直接在元素上使用该函数:
normalize-space(//div[@class='large-6 cell selectorgadget_selected'])产出:
AIRFRAME " Entry Into Service: December 2010" " Total Time Since New: 3,58@ Hours" " Total Landings Since New: 1,173" " (as of September 2019)" " Program Coverage: Enrolled on Smart Parts Plus" " Maintenance Tracking: CAMP "然后,要提取值,可以使用regex:
import re
text = 'AIRFRAME " Entry Into Service: December 2010" " Total Time Since New: 3,58@ Hours" " Total Landings Since New: 1,173" " (as of September 2019)" " Program Coverage: Enrolled on Smart Parts Plus" " Maintenance Tracking: CAMP "'
data = [el.strip() for el in re.findall(':(.+?)\"', text, re.IGNORECASE)]
print(data)产出:
['December 2010', '3,58@ Hours', '1,173', 'Enrolled on Smart Parts Plus', 'CAMP']https://stackoverflow.com/questions/62780252
复制相似问题