开发者

XPath to find anchor with specific domain in this HTML

开发者 https://www.devze.com 2023-02-05 18:21 出处:网络
I have the following HTML <div id=\"profile-bio-full\"> <p>Bla bla bla bla </p> <p>Site: <a href=\"http://www.something.com\" rel=\"nofollow\">something.com</a><

I have the following HTML

<div id="profile-bio-full">
  <p>Bla bla bla bla </p>
  <p>Site: <a href="http://www.something.com" rel="nofollow">something.com</a></p>
  <p>Facebook: <a href="http://www.facebook.com" rel="nofollow">facebook.com</a></p>
  <p>Twitter: <a href="http://www.twitter.com" rel="nofollow">www.twitter.com</a></p>
</div>

And i need to get the Twitter URL (the href "a" property).

I'm usi开发者_StackOverflow社区ng Rails with Nokogiri gem, and using Nokogiri xPath funciton.

I'm using this xPath

//div[contains(@id, "profile-bio-full")]/a[contains(@href, "twitter.com")]

But don't work :( . Any guesses?


Your single forward slash before your a matcher specifies that the a element needs to be an immediate child of div -- but it's not, it's a child of p.

You can either do this:

//div[contains(@id, "profile-bio-full")]/p/a[contains(@href, "twitter.com")]

Or you can just change /a to //a to mean the a just has to be a descendant rather than an immediate child.

//div[contains(@id, "profile-bio-full")]//a[contains(@href, "twitter.com")]
0

精彩评论

暂无评论...
验证码 换一张
取 消