如何使用'xpath'在HTML中提取我想要的东西

时间:2018-07-16 01:39:16

标签: html xpath

html代码看起来像这样:

<img alt="Papa&#39;s Cupcakeria To Go!" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-old-hires=""  class="a-dynamic-image  a-stretch-vertical" id="landingImage" data-a-dynamic-image="{&quot;https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L.png&quot;:[512,512],&quot;https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SX425_.png&quot;:[425,425],&quot;https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SX466_.png&quot;:[466,466],&quot;https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SY450_.png&quot;:[450,450],&quot;https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SY355_.png&quot;:[355,355]}" style="max-width:512px;max-height:512px;">

我想获得“ https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L.png”,现在我正在使用

extract_item(hxs.xpath("//img[@id='landingImage']/@data-a-dynamic-image"))

,我得到的是该标签内的所有内容。 如何仅获取第一个网址?

1 个答案:

答案 0 :(得分:0)

如果您只想要第一个网址:

full_content = extract_item(hxs.xpath("//img[@id='landingImage']/@data-a-dynamic-image"))
list_contents = full_content.split(";")
first_image = list_contents[1].replace("&quot","")
print first_image

此外,您可以参考this来使用正则表达式提取URL。