简体   繁体   English

XPath:从属性字符串中选择特定项目

[英]XPath: Select specific item from property string

Trying to drill down to a specific Xpath of a url in a longer string. 尝试向下钻取较长字符串中的URL的特定Xpath。 I've gotten down to each of the listed blocks, but can't seem to get any further than the long string of properties. 我已经深入到列出的每个块,但是似乎除了长长的属性字符串之外别无他法。

example code: 示例代码:

<div class="abc class">
    <a class="123" title="abc" keys="xyz" href="url string">

Right now I have... 现在我有...

.//*[@id='content']/div/div[1]/a

That only retrieves the whole string of data, from class through href. 那只会从类到href检索整个数据字符串。 What would I need to just retrieve the "url string" from that part? 我仅需要从该部分检索“ URL字符串”,该怎么办? Would this need to be accomplished with a subsequent 'for' argument in the python input? 是否需要在python输入中使用后续的“ for”参数来实现?

A pure XPath solution would involve just adding the @href to the expression: 一个纯XPath解决方案将只需要在表达式中添加@href

.//*[@id='content']/div/div[1]/a/@href

In Python, assuming you are using lxml.html , you can get the attribute using the .attrib : 在Python中,假设你使用lxml.html ,您可以用得到的属性.attrib

for link in root.xpath(".//*[@id='content']/div/div[1]/a"):
    print(link.attrib['href'])

Try to avoid this array 尽量避免这个数组

if your class name is unique you can do it like:- 如果您的班级名称是唯一的,则可以这样做:

//*[@id='content']/div/div[@class='abc class']/a[@keys='xyz']/@href

Hope it will help you :) 希望它能对您有所帮助:)

Xpath:select 内容来自特定<span>内部</span><div></div><div id="text_translate"><p>我有以下源代码:</p><pre> &lt;div class = "price-text--price-part--Tu6MH udlite-clp-discount-price udlite-heading-xxl" data-purpose = "course-price-text"&gt; &lt;span class = "udlite-sr-only"&gt; Current Price &lt;/span&gt; &lt;span&gt; &lt;span&gt; 47.99 &lt;/span&gt; &lt;/span&gt; &lt;/div&gt;</pre><p> 现在我想得到结果:47.99 使用 xpath。 我怎样才能做到这一点?</p></div> - Xpath: select content from a specific <span> inside an <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 xpath string() 从渲染中排除特定节点 - xpath string() exclude specific nodes from the rendering Scrapy Xpath 从 file-extension.net 中选择特定元素 - Scrapy Xpath select specific element from file-extension.net Xpath:select 内容来自特定<span>内部</span><div></div><div id="text_translate"><p>我有以下源代码:</p><pre> &lt;div class = "price-text--price-part--Tu6MH udlite-clp-discount-price udlite-heading-xxl" data-purpose = "course-price-text"&gt; &lt;span class = "udlite-sr-only"&gt; Current Price &lt;/span&gt; &lt;span&gt; &lt;span&gt; 47.99 &lt;/span&gt; &lt;/span&gt; &lt;/div&gt;</pre><p> 现在我想得到结果:47.99 使用 xpath。 我怎样才能做到这一点?</p></div> - Xpath: select content from a specific <span> inside an <div> XPATH, Select 来自 Flex 容器的特定有效值 - XPATH, Select an specific valid value from a Flex container 从xpath节点获取字符串的特定部分,不包含破折号 - Get specific part of string from xpath node without dashes python ...如何从字符串中获取特定项目和它之前的项目[暂停] - python … how to get specific item from a string and item before it [on hold] Python 2.7:从字符串中选择特定字符 - Python 2.7: Select specific character from string 如何从字符串中获取 select 特定字符串? - How to select specific string from strings? 如何使用此xpath从Web商店中选择其他项目进行Selenium测试? - How to select another item from web store using this xpath for Selenium test? 如何使用 Xpath 为机器人框架 select 下拉列表中的列表项 Selenium Python - How to select a list item from the dropdown list using Xpath for Robot framework Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM