【问题标题】:How to Xpath from Form如何从表单中获取 Xpath
【发布时间】:2022-01-25 17:22:08
【问题描述】:

我有类似的html代码:

<form class="variations_form cart" action="https://example.com/name-of-product" method="post" enctype='multipart/form-data' data-product_id="386" data-product_variations="[{&quot;attributes&quot;:{&quot;attribute_pa_czas-realizacji&quot;:&quot;24h&quot;},&quot;availability_html&quot;:&quot;&lt;p class=\&quot;stock out-of-stock\&quot;&gt;Brak w magazynie&lt;\/p&gt;\n&quot;,&quot;backorders_allowed&quot;:false,&quot;dimensions&quot;:{&quot;length&quot;:&quot;&quot;,&quot;width&quot;:&quot;&quot;}]">

我想提取“Brak w magazynie”。

我已经尝试过 xpath:

//*[text() = 'Brak w magazynie']

但它不起作用。知道怎么做吗? :)

【问题讨论】:

  • 您共享的元素 HTML 不包含该文本。如果要提取文本,则不应搜索具有已知文本的元素。你能分享一个指向那个网页的链接吗?
  • 表单属性“data-product_variations”具有该字符串。如果出现此字符串,则该产品在网站上缺货。
  • 那么,您要选择页面上有此指示的产品吗?好的,我会更新我的答案
  • 你能帮我@Prophet 这个案子 - stackoverflow.com/questions/71513109/… 吗?
  • 我看到了你的问题。我不确定我是否理解/可以提供帮助

标签: html forms web-scraping xpath


【解决方案1】:

您可以使用以下 XPath 表达式来定位此元素:

//form[@class='variations_form cart']

或者

//form[@action='https://example.com/name-of-product']

或者

//form[@action='https://example.com/name-of-product' and @class='variations_form cart']

然后提取找到的元素文本
UPD
如果您想选择在其data-product_variations 属性中包含Brak w magazynie 的此类元素,您可以像这样使用XPath:

//form[@class='variations_form cart' and(contains(@data-product_variations,'Brak w magazynie')) ]

或者

//form[@action='https://example.com/name-of-product' and contains(@data-product_variations,'Brak w magazynie')]

【讨论】:

  • 不,我不能。我有一个产品清单。其中一些有“Brak w magazynie”并且缺货,但其他没有该字符串并且可用。
  • 我明白了,请查看更新后的答案
  • 它有效 :D 非常感谢你 :) 你让我很开心 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 2014-06-13
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
相关资源
最近更新 更多