开发者

XPath to find anchor with specific domain in this HTML

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜