【问题标题】:How to extract something I want in html using 'xpath'如何使用'xpath'在html中提取我想要的东西
【发布时间】:2018-07-16 12:57:32
【问题描述】:

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"))

,我得到的是该标签内的所有内容。 我怎样才能只获得第一个网址?

【问题讨论】:

  • 更努力地处理。
  • @IgnacioVazquez-Abrams 你能说得更具体点吗?抱歉,我不熟悉前端的东西。

标签: html xpath


【解决方案1】:

如果你只想要第一个 URL:

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。

【讨论】:

  • 如果我的回答解决了您的问题,请点击大勾号接受它作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多