【发布时间】:2015-11-19 10:01:53
【问题描述】:
我正在抓取屏幕http://www.weather.com/weather/hourbyhour/l/INXX0202:1:IN。
我尝试选择同时使用 CSS 和 XPath 来获取网站中表格的降水预报部分。
它们都不能在我的程序中工作,因为它们返回空数组,但是,它们都可以在 Chrome 开发工具中工作(检查元素 -> 控制台 -> CSS 为 $$,Xpath 为 $x)。
为什么会这样?跟命名空间有关系吗?
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 []
这是a screenshot of the website,我正在尝试从中获取数据。我想要的是“Precip”标题下的数字(例如:图片中的 85,100,100,95,80,70,45,40)。
我将页面的 HTML 复制到本地 HTML 文件中,并让我的程序访问该文件。然后程序给了我所需的输出,但是当我让同一个程序使用 OpenUri 访问网站时,它返回一个空数组:
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 的 sn-p(显示的部分嵌套在网站的多个标签中):
<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>
【问题讨论】:
-
欢迎来到 Stack Overflow。请不要让我们去一个站点并挖掘 HTML;您需要提取 minimal HTML 来演示您正在使用的内容并将其放入您的问题中。向我们提供您要收集的数据的示例。请阅读“How to Ask”。使用短选择器比长选择器更容易定位数据。将其分解成小块,看看您是否可以通过这种方式获取数据。
-
@theTinMan 感谢您的意见。请看一下我编辑的帖子。为了准确起见,我的选择器有点长,并且只提取我需要的数据。然而,话虽如此,我会寻找更短、更好的方法来获取相同的数据。我尝试分解选择器,但这也不起作用。
标签: css ruby xpath nokogiri open-uri