gpt4 book ai didi

xpath - 使用Xpath和Nokogiri选择特定的div元素?

转载 作者:行者123 更新时间:2023-12-03 16:54:29 25 4
gpt4 key购买 nike

我对解析相对较新,并且希望获得更多练习。我想解析以下URL:http://www.goodreads.com/quotes/tag/hard-work

我想抓住所有标记为“努力”的报价。这是站点代码分解为的内容:

<div class="content">
<div id="siteheader" class="uitext">
<div class="mainContentContainer ">
<div class="mainContent">
<div id="premiumAdTop">
<div class="mainContentFloat">
<div id="flashContainer"> </div>
<div id="connectPrompt" style="">
<img style="float: left; margin: -3px 5px 0px 0px" src="http://s.gr-assets.com/assets/quote/quote_tiny-566b7de5e1ac5becd0dd8b2856f59228.jpg" alt="quote">
<h1>Quotes About Hard Work</h1>
<div class="leftContainer">
<div class="mediumText">
<div class="quote mediumText ">
<div class="quoteDetails ">
<a class="leftAlignedImage" href="/author/show/3916262.Babe_Ruth">
<div class="quoteText">
“It's hard to beat a person who never gives up.”
<br>

<a href="/author/show/3916262.Babe_Ruth">Babe Ruth</a>
</div>


现在我的代码是:

require "rubygems"
require "open-uri"
require "nokogiri"

@page = Nokogiri::HTML(open("http://goodreads.com/quotes"))
@div = @page.xpath("html/body/div[1]")


但是结果并没有给我想要的输出。

我认为应该将方法称为 eachcollect,但我只是不知道如何到达所需的节点,我认为它包含在此处:

<div id="connectPrompt" style="">
<img style="float: left; margin: -3px 5px 0px 0px" src="http://s.gr-assets.com/assets/quote/quote_tiny-566b7de5e1ac5becd0dd8b2856f59228.jpg" alt="quote">
<h1>Quotes About Hard Work</h1>
<div class="leftContainer">
<div class="mediumText">
<div class="quote mediumText ">
<div class="quoteDetails ">
<a class="leftAlignedImage" href="/author/show/3916262.Babe_Ruth">
<div class="quoteText">
“It's hard to beat a person who never gives up.”
<br>

<a href="/author/show/3916262.Babe_Ruth">Babe Ruth</a>
</div>


有人能指出我正确的方向吗?我必须进入div类多远才能获得想要的东西?

最佳答案

您可以使用XPath:

//div[@class = 'quoteText' and following-sibling::div[1][@class = 'quoteFooter' and .//a[@href and normalize-space() =  'hard-work']]]


选择所有 div元素,这些元素的类为 quoteText,后跟具有类 divquoteFooter,其中包含与 hard-work的链接。

关于xpath - 使用Xpath和Nokogiri选择特定的div元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20598080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com