- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序,使用 JSoup 从 weather.com 获取天气信息。这是我目前所拥有的:
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.Jsoup;
public class WeatherComHandler {
private Element ForeCastDiv;
public static void main(String[] args) {
WeatherComHandler wch = new WeatherComHandler("http://www.weather.com/weather/today/Brussels+BEXX0005:1:BE");
wch.getTodaysForeCast();
}
public WeatherComHandler(String url) {
getForeCastDiv(url);
}
private void getForeCastDiv(String url) {
try {
Document doc = Jsoup.connect(url).get();
ForeCastDiv = doc.getElementById("wx-forecast-container");
} catch(Exception e) {
e.getStackTrace();
}
}
public void getTodaysForeCast() {
System.out.println(ForeCastDiv.toString());
System.out.println(ForeCastDiv.hasClass("wx-data-part wx-first"));
Elements todaysElements = ForeCastDiv.getElementsByClass("wx-data-part wx-first");
System.out.println(todaysElements.size());
}
}
这是输出:
<div id="wx-forecast-container">
<div class="wx-today wx-module wx-grid3of6">
<div class="wx-title-container">
<h2 class="wx-title"><img src="http://i.imwx.com/common/My.png" /> Forecast for Today</h2>
<div class="wx-updated">
Updated: Oct 28, 5:45pm Local Time
</div>
<div class="wx-promo-links wx-cond wx-severe wx-severe-0">
<ul class="wx-links">
<li class="wx-icon wx-cond wx-desktop"> <a class="wx-icon wx-cond wx-desktop" href="/apps/desktop-weather" location="" from="today_promo_1" title="Weather on my Desktop">Desktop App</a> </li>
</ul>
</div>
</div>
<div class="wx-weather-details">
<div class="wx-timepart-title wx-first">
Right Now
<div class="wx-module wx-grid1of6 wx-weather wx-love-module wx-cond-bg">
<div class="wx-module-inner">
<div class="wx-icon wx-social wx-balloon-wt-lt wx-clickable wx-love-ugh" data-good="true" data-share-trx="" data-share-icon="http://s.imwx.com/img/common/social/lmw-love.jpg" title="Love My Weather" data-share-desc="It's 55°F, Clear">
<div class="wx-icon wx-social wx-heart"></div>
<h4 data-share-title="Love! Right now in Brussels, Belgium"></h4>
</div>
<div class="wx-icon wx-social wx-balloon-wt-rt wx-clickable wx-love-ugh" data-good="false" data-share-trx="" data-share-icon="http://s.imwx.com/img/common/social/lmw-ugg.jpg" title="Don't Love My Weather" data-share-desc="It's 55°F, Clear">
<h4 data-share-title="Ugh! Right now in Brussels, Belgium"></h4>
</div>
</div>
</div>
</div>
<div class="wx-timepart-title">
Earlier Today
</div>
<div class="wx-timepart-title">
Tonight
</div>
<div class="wx-data-part wx-first">
<img src="http://s.imwx.com/v.20131006.214956/img/wxicon/120/31.png" height="120" width="120" alt="Clear" class="wx-weather-icon" />
</div>
<div class="wx-data-part">
<img src="http://s.imwx.com/v.20131006.214956/img/wxicon/120/34.png" height="120" width="120" alt="Fair / Windy" class="wx-weather-icon" />
</div>
<div class="wx-data-part">
<img src="http://s.imwx.com/v.20131006.214956/img/wxicon/120/29.png" height="120" width="120" alt="Partly Cloudy" class="wx-weather-icon" />
</div>
<div class="wx-data-part wx-first">
<div class="wx-temperature">
<span itemprop="temperature-fahrenheit">55</span>
<span class="wx-degrees">°<span class="wx-unit">F</span></span>
</div>
<div class="wx-temperature-label">
FEELS LIKE
<span itemprop="feels-like-temperature-fahrenheit">55</span>°
</div>
</div>
<div class="wx-data-part">
<div class="wx-temperature">
62
<span class="wx-degrees">°</span>
</div>
<div class="wx-temperature-label">
HIGH AT 11:45 AM
</div>
</div>
<div class="wx-data-part">
<div class="wx-temperature">
47
<span class="wx-degrees">°</span>
</div>
<div class="wx-temperature-label">
LOW
</div>
</div>
<div class="wx-data-part wx-first">
<div class="wx-phrase ">
Clear
</div>
<div class="wx-obs-qualifier ">
<span class="wx-hide" itemprop="observation-qualifier">OQ1190</span>
<span class="wx-hide" itemprop="observation-qualifier-severity">2</span>
<span itemprop="observation-qualifier-phrase">Locations nearby are reporting rain with wind.</span>
</div>
</div>
<div class="wx-data-part">
<div class="wx-phrase ">
Fair / Windy
</div>
</div>
<div class="wx-data-part">
<div class="wx-phrase ">
Partly Cloudy
</div>
</div>
<div class="wx-data-part wx-first">
<h6 class="wx-label">Past 24-hr Precip:</h6>
<div class="wx-data">
0.34 in
<span class="wx-estimated">(est.)</span>
</div>
</div>
<div class="wx-data-part">
<h6 class="wx-label"></h6>
<div class="wx-data"></div>
</div>
<div class="wx-data-part">
<h6 class="wx-label">Chance of <span class="wx-firstletter">rain</span></h6>
<div class="wx-data">
<span class="wx-icon wx-raindrop"></span>20%
</div>
</div>
<div class="wx-collapsible">
<div class="wx-data-part wx-first wx-wind">
<h6 class="wx-label">Wind:</h6>
<div class="wx-data">
<div class="wx-icon wx-dir-arrow-sm wind-dir-SW"></div>
<div class="wx-wind-label">
SW at 17 mph
</div>
</div>
</div>
<div class="wx-data-part wx-wind">
<h6 class="wx-label"></h6>
<div class="wx-data">
<div class="wx-wind-label"></div>
</div>
</div>
<div class="wx-data-part wx-wind">
<h6 class="wx-label">Wind:</h6>
<div class="wx-data">
<div class="wx-icon wx-dir-arrow-sm wind-dir-SW"></div>
<div class="wx-wind-label">
SW at 18 mph
</div>
</div>
</div>
<div class="wx-data-part wx-first">
<h6 class="wx-label">Humidity:</h6>
<div class="wx-data">
76%
</div>
</div>
<div class="wx-data-part">
<h6 class="wx-label"></h6>
<div class="wx-data"></div>
</div>
<div class="wx-data-part">
<h6 class="wx-label">Humidity:</h6>
<div class="wx-data">
82%
</div>
</div>
<div class="wx-data-part wx-first">
<h6 class="wx-label">UV Index:</h6>
<div class="wx-data">
0 - Low
</div>
</div>
<div class="wx-data-part">
<h6 class="wx-label"></h6>
<div class="wx-data"></div>
</div>
<div class="wx-data-part"></div>
<div class="wx-data-part wx-first ">
<h6 class="wx-label"></h6>
<p class="wx-text"></p>
</div>
<div class="wx-data-part ">
<h6 class="wx-label"></h6>
<p class="wx-text"></p>
</div>
<div class="wx-data-part ">
<h6 class="wx-label">Tonight:</h6>
<p class="wx-text">Partly cloudy skies. Low 47F. Winds SW at 15 to 25 mph.</p>
</div>
<div class="wx-astro-neighbor">
<div class="wx-data-part wx-first">
<h6 class="wx-label">Pressure:</h6>
<div class="wx-data">
<span itemprop="barometric-pressure-incheshg"> 29.59 in </span>
<span class="wx-icon wx-dir-arrow-sm wx-pressure-steady"></span>
</div>
</div>
<div class="wx-data-part wx-first wx-dewpoint">
<h6 class="wx-label">Dew Point: <span class="wx-icon wx-long wx-info"> <i>Humidity becomes more noticeable as the dew point rises above 60°F. Readings above 75°F can feel stifling.</i> </span> </h6>
<div class="wx-data">
47°
</div>
</div>
<div class="wx-data-part wx-first">
<h6 class="wx-label">Visibility:</h6>
<div class="wx-data">
6.0 mi
</div>
</div>
</div>
<div class="wx-astro-details">
<h3 class="wx-astro-title">Sun & Moon</h3>
<dl class="wx-first">
<dt>
Sunrise:
</dt>
<dd>
7:28 am
</dd>
<dt>
Sunset:
</dt>
<dd>
5:25 pm
</dd>
</dl>
<dl>
<dt>
Moonrise:
</dt>
<dd>
12:27 am
</dd>
<dt>
Moonset:
</dt>
<dd>
2:32 pm
</dd>
</dl>
<dl class="wx-moonphase-container">
<dt>
Moonphase:
</dt>
<dd>
<div class="wx-icon wx-moonphase wx-moon-23"></div>
<div class="wx-moonphase-label">
Waning Crescent
</div>
</dd>
</dl>
</div>
</div>
</div>
<div class="wx-toggle-collapsible-container">
<div class="wx-toggle-collapsible wx-clickable">
<div class="wx-control wx-expand-control" tracktype="action" trackstr="today_rightnow_showmore">
<span class="wx-icon wx-arrow wx-mb-lg-dn"></span>Open Weather Details
</div>
<div class="wx-control wx-collapse-control wx-hide" tracktype="action" trackstr="today_rightnow_showless">
<span class="wx-icon wx-arrow wx-mb-lg-up"></span>Close Weather Details
</div>
</div>
</div>
<div class="wx-pagination-linkers">
<div class="wx-next-linker">
<ul class="wx-links">
<li class=""> <a class="" href="/weather/hourbyhour/graph/Brussels+BEXX0005:1:BE" location="" from="today_rightnow-Today_tempGraph_1" title="">Hourly Forecast</a> </li>
</ul>
</div>
</div>
</div>
<script>$("#wx-icon wx-long wx-info").attr('title','This is the hover-over text');</script>
<div id="wunderground-lightbox" class="wx-dialog">
<div class="wx-close" title="Close">
<div class="wx-icon"></div>
</div>
</div>
<div class="wx-combinedPromo wx-module wx-grid3of6">
<div class="wx-commuter-promo" data-classname="wx-grid-2" from="today-commuterpromo" data-layers="traffic,weather" data-locationinus="false" data-backgroundimageurl="" data-location-zoom="10" data-location-latitude="50.85045" data-location-longitude="4.34878">
<div class="wx-commute-promo-callout-title">
<div class="wx-titleIconHolder">
<img class="wx-titleIcon" src="http://s.imwx.com/img/module/TrafficCams_16x16.png" />
</div>
<div class="wx-titleText">
NEW! Traffic & Commuter Forecast
</div>
</div>
</div>
</div>
<div class="wx-clear"></div>
<div class="wx-scroll-tracking wx-scroll-pres" data-modulecode="ScrollTracking1_Today" data-location="BEXX0005:1:BE" data-groupname="weblocal"></div>
<div class="wx-free-title ">
<h3>Latest Stories</h3>
</div>
<div id="wx-rightnowsecond-container">
<div id="wx-rightnow-container">
<div class="wx-module wx-mod1 wx-corsican wx-1x155M wx-render-mode" id="mod_4" data-vr-zone="today_beta_Media_top2">
<div class="wx-inner" data-vr-contentbox=""></div>
</div>
<div class="wx-module wx-mod1 wx-corsican wx-1x155M wx-render-mode" id="mod_5" data-vr-zone="today_beta_Media_top1">
<div class="wx-inner" data-vr-contentbox=""></div>
</div>
</div>
<div class="wx-module wx-trending-stories wx-tabbed-content wx-grid2of6">
<div class="wx-inner">
<h2 class="wx-title">Most Popular</h2>
<ul class="wx-tabs" data-vr-zone="Most Popular">
<li class="wx-tab wx-active" data-tab="today" data-vr-contentbox="">Today</li>
<li class="wx-tab wx-clickable" data-tab="this_week" data-vr-contentbox="">This Week</li>
</ul>
<ul class="wx-tab-content" data-tab="today" data-vr-zone="Most Popular">
<li data-vr-contentbox=""><a href="/article/superstorm-sandy-photos-then-and-now-20131024" from="62288_MostPopular_1">Superstorm Sandy: Then and Now (EXCLUSIVE PHOTOS)</a></li>
<li data-vr-contentbox=""><a href="/article/eerie-nighttime-shots-abandoned-america-20131023" from="62288_MostPopular_2">Eerie Nighttime Shots of Abandoned America</a></li>
<li data-vr-contentbox=""><a href="/article/fall-foliage-photos-20131021" from="62288_MostPopular_3">Astounding Fall Photos That Will Make You Say 'Wow!'</a></li>
<li data-vr-contentbox=""><a href="/article/nightmares-fear-factory-terrified-reactions-haunted-house-photos-20131001" from="62288_MostPopular_4">Nightmares Fear Factory: Terrified Reactions at Haunted House (PHOTOS)</a></li>
<li data-vr-contentbox=""><a href="/article/2013s-worst-cities-asthma-20131025" from="62288_MostPopular_5">The 15 Worst U.S. Cities for Asthma</a></li>
<li data-vr-contentbox=""><a href="/video/shocking-end-to-shark-attack-39402" from="62288_MostPopular_6">Shocking End to Shark Attack <span class="wx-icon wx-video wx-trending-video"></span></a></li>
<li data-vr-contentbox=""><a href="/video/gigantic-hornets-are-breeding-39247" from="62288_MostPopular_7">Gigantic Hornets Are Breeding <span class="wx-icon wx-video wx-trending-video"></span></a></li>
<li data-vr-contentbox=""><a href="/video/18-foot-what-39575" from="62288_MostPopular_8">18-Foot WHAT? <span class="wx-icon wx-video wx-trending-video"></span></a></li>
</ul>
<ul class="wx-tab-content" data-tab="this_week" style="display:none;" data-vr-zone="Most Popular">
<li data-vr-contentbox=""><a href="/article/superstorm-sandy-photos-then-and-now-20131024" from="62288_MostPopular_1">Superstorm Sandy: Then and Now (EXCLUSIVE PHOTOS)</a></li>
<li data-vr-contentbox=""><a href="/article/nightmares-fear-factory-terrified-reactions-haunted-house-photos-20131001" from="62288_MostPopular_2">Nightmares Fear Factory: Terrified Reactions at Haunted House (PHOTOS)</a></li>
<li data-vr-contentbox=""><a href="/article/eerie-nighttime-shots-abandoned-america-20131023" from="62288_MostPopular_3">Eerie Nighttime Shots of Abandoned America</a></li>
<li data-vr-contentbox=""><a href="/article/deadly-venomous-creatures-20131022" from="62288_MostPopular_4">8 Deadly Venomous Creatures and Those Who've Survived (PHOTOS)</a></li>
<li data-vr-contentbox=""><a href="/article/fall-foliage-photos-20131021" from="62288_MostPopular_5">Astounding Fall Photos That Will Make You Say 'Wow!'</a></li>
</ul>
</div>
</div>
</div>
<div class="wx-map wx-cc-map wx-grid3of6 wx-module pagelet " data-severelocation="false" data-location="BEXX0005" data-location-type="1" data-overlay="" data-zoom="" data-tropical="" data-mapsize="3" data-fragid="frag_newccMap_166739" data-modulecode="beta_CCMap" data-pageid="62288">
</div>
</div>
false
0
问题是 getTodaysForeCast()
一直说 todaysElements
的大小是 0,这不应该是。任何人都可以向我解释为什么会发生这种情况或我做错了什么吗?
提前致谢。
最佳答案
Jsoup 不会在 getElementsByClass
方法中接受两个组合类。尝试类似的东西:
Elements todaysElements = ForeCastDiv.getElementsByAttributeValueContaining("class", "wx-data-part wx-first");
关于java - Jsoup getElementsByClass 给出大小为 0 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644603/
我有一些像这样的 html: zip code 我的 Java 代码 Elements formElements = doc.getElementsByTag("form"); for(Ele
我无法使用 创建 session jsoup 以及如何使用 jsoup 发布数据.请帮助我,我是新来的 jsoup api ,实际上我的代码是: Connection.Response res = J
我想添加一个新的元标记 Document doc = Jsoup.parse(.....) doc.select("meta").first.appendElement("meta".attr("na
有没有办法用 Jsoup 保留新行,(不是 )? Document pdsc = Jsoup.connect("http://drafts.bestsiteeditor.com/promoters/d
我需要将 jsoup 元素映射回源 HTML 中的特定字符偏移量。换句话说,如果我有这样的 HTML: Hello World 我需要知道“Hello”从偏移量 0 开始,长度为 6 个字符,从偏移
我喜欢用 Jsoup 解析 html,但是他们的连接有问题,我需要将请求发送到同一个网站但不同的查询参数,比如“id=XXX”,请求是这样的: http://website/?id=XXX 我不想为每
我有代码,有点像这样 String str = " >foo< "; Document doc = Jsoup.parse(str, "", Parser.xmlParser()); 但
是否可以使用 jsoup 来验证 HTML 片段?我想知道标记是否格式错误,而不是让 jsoup 自动修复它,我希望能够通知用户自己修复源标记。 最佳答案 Jsoup 不是检查 xml 或 html
Jsoup 有 2 个 html parse() 方法: > parse(String html) - "由于没有指定基本 URI,绝对 URL检测依赖于包含标记的 HTML。” > parse(St
我正在尝试使用 jsoup 从此网页中提取所有图片网址?任何人都可以提供有关如何做到这一点的帮助吗?所有标签的格式都是这样的,但我只需要 src 图像,而不是 ajaxsrc: 链接在这里: htt
我试图找到所有 或 一页/文档中的标签。 我如何使用 OR运算符(operator)在 doc.select("div.name1 OR div.name2") ? 最佳答案 select metho
我为我的项目创建了一个新模块来添加一些额外的功能。在该模块中,我在模块的 Gradle 文件 implementation 'org.jsoup:jsoup:1.10.2' 中添加了 Jsoup 依赖
我正在寻找这个 div 中的主图像 我试过这个: Document document = Jsoup.connect(url).get(); Elements img = document.se
谁能解释一下 JSoup 中提供的 Element 对象和 Node 对象之间的区别? 在什么情况/条件下使用什么最好。 最佳答案 节点是 DOM 层次结构中任何类型对象的通用名称。 元素是一种特定类
有什么方法可以防止 Jsoup 的 HTML 解析器将单个标签(最具体的是 标签)转换为自闭合标签? 标签是有效的 HTML5 元素,但 Jsoup 一直将它们转换为 . 我在下面的链接中有一个示
可以屏蔽吗 Jsoup.connect("http://xyz.com").get().html(); 作为对网站的浏览器调用? 我尝试构建一个壁纸下载工具,但在从服务器下载页面时遇到问题。 如果我下
我希望在 Groovy 中开发一个网络爬虫(使用 Grails 框架和 MongoDB 数据库),它能够爬取网站,创建网站 URL 列表及其资源类型、内容、响应时间和所涉及的重定向数量。 我正在讨论
如果我有一个看起来像这样的元素: bar text 1 bar text 2 我已经有了 元素被选中,我想选择 元素是 的直接子元素但不是
任何人都可以提供有关我将如何解析超大 HTML 流/文件的指针或建议。例如,我有一个大约有 270,000 行的表,我想一次将它带入我的应用程序大约 20,000 行。 jsoup 解析方法允许使用
我收到此错误: java.lang.RuntimeException: An error occured while executing doInBackground() at and
我是一名优秀的程序员,十分优秀!