首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nokogiri返回空数组

Nokogiri返回空数组
EN

Stack Overflow用户
提问于 2015-11-19 10:01:53
回答 1查看 942关注 0票数 2

我在屏幕上刮http://www.weather.com/weather/hourbyhour/l/INXX0202:1:IN

我尝试使用CSS和XPath来选择网站中的降水预报部分。

它们在我的程序中都不起作用,因为它们返回空数组,但是,它们都在Chrome工具中工作(检查元素-> console -> $$ for CSS,$x for Xpath)。

为什么会发生这种情况?它与名称空间有关吗?

代码语言:javascript
复制
require 'open-uri'
require 'nokogiri'
foo = Nokogiri::HTML(open("http://www.weather.com/weather/hourbyhour/l/INXX0202:1:IN"))
foo.remove_namespaces!
p foo.xpath("//section[@data-ng-class]/p[@class='precip weather-cell ng-isolate-scope']/span[@data-ng-if]") # returns []
p foo.css("section[data-ng-class] p[class='precip weather-cell ng-isolate-scope'] span[data-ng-if]")  # returns []

下面是我正在尝试从网站截图获取数据的方法。我想要的是"Precip“标题下的数字(例如:图中的85,100,100,95,80,70,45,40 )。

我将页面的HTML复制到一个本地HTML文件中,并让我的程序访问该file.The程序,然后给我所需的输出,但是当我使用OpenUri访问同一个程序访问网站时,它返回一个空数组:

代码语言:javascript
复制
require 'open-uri'
require 'nokogiri'
foo = open("http://www.weather.com/weather/hourbyhour/l/INXX0202:1:IN")
nokogirifoo = Nokogiri::HTML(foo)
p nokogirifoo.xpath("//section[@data-ng-class]/p[@class='precip weather-cell ng-isolate-scope']/span[@data-ng-if]") # => empty array

bar = File.open('weather.html') # weather.html is just the html code of the page copied into a local file
nokogiribar = Nokogiri::HTML(bar)
p nokogiribar.xpath("//section[@data-ng-class]/p[@class='precip weather-cell ng-isolate-scope']/span[@data-ng-if]").text # => "85%100%100%95%80%70%45%40%" (this is what I need)

下面是HTML的一个片段(所显示的部分嵌套在网站中的多个标记中):

代码语言:javascript
复制
 <section class="wxcard-hourly summary-view ng-isolate-scope last" data-ng-class="{'last': $last}" data-wxcard-hourly="hour" data-wxcard-hourly-methods="hourlyScope" data-hours-index="hoursDataIndex" data-show-wx-labels="false" data-details-view="false">
    <div class="heading weather-cell" data-ng-switch="dataMethods.checkTime(data.getForecastLocalDate())">
        <h2>

      <span class="wx-dsxdate ng-binding ng-scope" ng-bind-template=" 9:30 am" data-dsxdate="" data-ng-switch-when="min" data-datetime="data.getForecastLocalDate()" data-timezone="locTz" data-format="'h:mm a'"> 9:30 am</span>
        </h2>
    <span class="sub-heading wx-hourly-date wx-dsxdate ng-binding ng-scope" ng-bind-template=" Fri, Nov 20" data-dsxdate="" data-datetime="data.getForecastLocalDate()" data-timezone="locTz" data-format="'EEE, MMM d'"> Fri, Nov 20</span>
    </div>
    <p class="hi-temp temp-1 weather-cell ng-isolate-scope" data-wx-temperature="data.getTemp()" data-show-temp-unit="hoursIndex === 0"> <span data-ng-if="hasValue()" data-ng-bind="temp" class="ng-binding ng-scope">28</span><sup data-ng-if="hasValue()" class="deg ng-scope">°</sup><sup class="temp-unit ng-binding ng-scope" data-ng-if="showTempUnit" data-ng-bind="tempUnit()">C</sup>
</p>
    <p class="feels-like temp-2 weather-cell ng-isolate-scope" data-wx-temperature="data.getFeelsLike()" data-temp-prefix="Feels"><span ng-if="tempPrefix" class="temp-prefix ng-binding ng-scope" data-ng-bind="tempPrefix">Feels</span><span data-ng-if="hasValue()" data-ng-bind="temp" class="ng-binding ng-scope">34</span><sup data-ng-if="hasValue()" class="deg ng-scope">°</sup>
</p>
    <div class="weather-cell">
        <h3 class="weather-phrase">
            <div class="weather-icon ng-isolate-scope wx-weather-icon" data-wxicon="" data-sky-code="data.getSkyCode()"><div class="svg-icon"><img src="/sites/all/modules/custom/angularmods/app/shared/wxicon/svgz/thunderstorm.svgz?1" aria-hidden="true" alt="thunderstorm"></div></div>

            <span class="phrase ng-binding" data-ng-bind-template="Thunderstorms">Thunderstorms</span>
        </h3>
    </div>
    <!-- The Next Line Is What I Need-->
    <p class="precip weather-cell ng-isolate-scope" data-wx-precip="dataMethods.roundedValue(data.getChanceOfPrecipDay())" data-wx-precip-type="data.getPrecipType()" data-wx-precip-sky-code="data.getSkyCode()"><span aria-hidden="true" class="wx-iconfont-global wx-icon-precip-rain-1"></span><span data-ng-if="!wxPrecipIconOnly" class="precip-val ng-binding ng-scope" data-ng-bind="chanceOfPrecip() | safeDisplay">85%</span></p>

    <p class="humidity-wrapper weather-cell">
      <span data-ng-bind-template="85%" class="humidity ng-binding ng-isolate-scope" data-wx-percentage="data.getHumidity()">85%</span>
    </p>

    <p class="wind-conditions weather-cell">
        <span class="wx-wind ng-binding ng-isolate-scope" data-ng-bind-template="ESE 9 km/h" data-wx-wind-direction="data.getWindDirectionText()" data-wx-wind-speed="data.getWindSpeed()">ESE 9 km/h</span>
    </p>
</section>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-20 19:01:28

问题在于,您正在使用浏览器查看页面,该页面除了实现HTML解析器外,还具有一个嵌入式JavaScript解释器。浏览器在为用户呈现页面之前,查找并处理任何JavaScript <script>标记,加载和调整元素。这就是你想要的页面上发生的事情。像Nokogiri这样的解析器不是浏览器,也不关心嵌入式脚本,因为在HTML中,脚本只是特定标记中的文本,因此,您想要的第二个HTML永远不会被检索。

您说您将HTML保存到一个文件中,但是,您没有说明如何保存它。我猜,因为保存的HTML包含您想要的信息,所以它是使用浏览器保存的。

在使用网页时,第一步就是确定页面是使用动态HTML和/或JavaScript,还是静态HTML。在浏览器中关闭JavaScript,并加载该URL。或者,您可以使用命令行中的wgetcurl检索页面并使用编辑器查看它。在这两种情况下,您看到您想要的内容吗?如果是这样的话,那么在检索到像Nokogiri这样的解析器之后,您就可以得到它的可能性。如果没有,则必须使用一些可以解释JavaScript的内容,处理加载的信息,然后将其传递给解析器。

像PhantomJS和Watir这样的工具可以帮助您,或者找到一个气象服务,它允许您使用API来检索数据而不刮擦,因为刮刮总是非常脆弱的。

还可以确定JavaScript用于检索数据的URL,然后请求该辅助资源并对其进行解析。它可能是HTML,也可能是包含数据的JSON,然后由JavaScript处理,然后动态构建整个表。

关于堆栈溢出有很多问题和答案,讨论如何做以上所有的事情。

也就是说,一旦获得了所需的HTML,就可以轻松地减少这些值所需的CSS选择器。每个值都封装在一个有类的<style>标记中,所以使用该类查找值。

代码语言:javascript
复制
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)

    <section class="wxcard-hourly summary-view ng-isolate-scope last" data-ng-class="{'last': $last}" data-wxcard-hourly="hour" data-wxcard-hourly-methods="hourlyScope" data-hours-index="hoursDataIndex" data-show-wx-labels="false" data-details-view="false">
        <div class="heading weather-cell" data-ng-switch="dataMethods.checkTime(data.getForecastLocalDate())">
            <h2>

          <span class="wx-dsxdate ng-binding ng-scope" ng-bind-template=" 9:30 am" data-dsxdate="" data-ng-switch-when="min" data-datetime="data.getForecastLocalDate()" data-timezone="locTz" data-format="'h:mm a'"> 9:30 am</span>
            </h2>
        <span class="sub-heading wx-hourly-date wx-dsxdate ng-binding ng-scope" ng-bind-template=" Fri, Nov 20" data-dsxdate="" data-datetime="data.getForecastLocalDate()" data-timezone="locTz" data-format="'EEE, MMM d'"> Fri, Nov 20</span>
        </div>
        <p class="hi-temp temp-1 weather-cell ng-isolate-scope" data-wx-temperature="data.getTemp()" data-show-temp-unit="hoursIndex === 0"> <span data-ng-if="hasValue()" data-ng-bind="temp" class="ng-binding ng-scope">28</span><sup data-ng-if="hasValue()" class="deg ng-scope">°</sup><sup class="temp-unit ng-binding ng-scope" data-ng-if="showTempUnit" data-ng-bind="tempUnit()">C</sup>
    </p>
        <p class="feels-like temp-2 weather-cell ng-isolate-scope" data-wx-temperature="data.getFeelsLike()" data-temp-prefix="Feels"><span ng-if="tempPrefix" class="temp-prefix ng-binding ng-scope" data-ng-bind="tempPrefix">Feels</span><span data-ng-if="hasValue()" data-ng-bind="temp" class="ng-binding ng-scope">34</span><sup data-ng-if="hasValue()" class="deg ng-scope">°</sup>
    </p>
        <div class="weather-cell">
            <h3 class="weather-phrase">
                <div class="weather-icon ng-isolate-scope wx-weather-icon" data-wxicon="" data-sky-code="data.getSkyCode()"><div class="svg-icon"><img src="/sites/all/modules/custom/angularmods/app/shared/wxicon/svgz/thunderstorm.svgz?1" aria-hidden="true" alt="thunderstorm"></div></div>

                <span class="phrase ng-binding" data-ng-bind-template="Thunderstorms">Thunderstorms</span>
            </h3>
        </div>
        <!-- The Next Line Is What I Need-->
        <p class="precip weather-cell ng-isolate-scope" data-wx-precip="dataMethods.roundedValue(data.getChanceOfPrecipDay())" data-wx-precip-type="data.getPrecipType()" data-wx-precip-sky-code="data.getSkyCode()"><span aria-hidden="true" class="wx-iconfont-global wx-icon-precip-rain-1"></span><span data-ng-if="!wxPrecipIconOnly" class="precip-val ng-binding ng-scope" data-ng-bind="chanceOfPrecip() | safeDisplay">85%</span></p>

        <p class="humidity-wrapper weather-cell">
          <span data-ng-bind-template="85%" class="humidity ng-binding ng-isolate-scope" data-wx-percentage="data.getHumidity()">85%</span>
        </p>

        <p class="wind-conditions weather-cell">
            <span class="wx-wind ng-binding ng-isolate-scope" data-ng-bind-template="ESE 9 km/h" data-wx-wind-direction="data.getWindDirectionText()" data-wx-wind-speed="data.getWindSpeed()">ESE 9 km/h</span>
        </p>
    </section>
EOT

从一个简单的搜索开始:

代码语言:javascript
复制
doc.at('.precip-val').text # => "85%"

at找到第一个匹配节点并返回它。text检索其文本节点.

您需要这个类的多个节点,所以类似这样的内容应该会有所帮助:

代码语言:javascript
复制
doc.search('.precip-val').map(&:text) # => ["85%"]

search找到所有匹配的节点并返回一个NodeSet,它类似于一个数组,可以使用map进行迭代。

它们不太可能使用.precip-val来包装值,但是,如果它们使用了,尝试:

代码语言:javascript
复制
doc.search('span.precip-val').map(&:text)

看看你能得到什么。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33800628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档