主要是,我的问题是如何解析......
<p>some text<br />
<br />
<strong>categorized: </strong>like this<br />
<br /></p>
...我最终有兴趣获得关键值对,例如&#34;分类&#34;,&#34;像这样&#34;使用Java和Jsoup?我在看&lt; strong&gt;标签是某种分隔符,我可以用它来表示密钥,然后它的后续文本不方便地不包含在我需要抓取的标签中作为值。
我认为我面临的挑战是&#34;像这样&#34;部分不在元素中。它是一个兄弟节点,但它不能用CSS选择,所以我无法用Jsoup找到它。我不清楚节点和元素关系如何在Jsoup中工作,这样我就可以得到元素文本&#34;分类&#34;和它的兄弟姐妹&#34;像这样&#34;在一个电话中。
更详细地说,由于我试图从许多消费者产品安全委员会网页收集数据,因此我无法控制HTML结构。页面的格式有几种不同的方式,但有一种格式特别导致我使用Java和Jsoup解析数据时出现问题。
<div class="archived">
<p style="text-align: center;"><strong><span style="color: #ff0000;">Note: The hotline number and ...</span></strong></p>
<h2 style="text-align: left;">CPSC, Elkay Manufacturing Co. Announces ...</h2>
<p>WASHINGTON, D.C. - The U.S. Consumer Product Safety Commission ...<br />
<br />
<strong>Name of product:</strong> Elkay hot/cold bottled water coolers <br />
<br />
<br />
<strong>Units:</strong> 145,000<br />
<br />
<strong>Description:</strong> These 115 volt hot/cold bottled water coolers ... <br />
<p><img title="Picture of Recalled Water Cooler" src="/PageFiles/73998/04175.jpg" alt="Picture of Recalled Water Cooler" width="110" height="434" /></p>
</div>
缩短了HTML的特定部分,但它来自http://www.cpsc.gov/en/Recalls/2004/CPSC-NETGEAR-Inc-Announce-Recall-of-Wall-Plug-Ethernet-Bridges-/
String url = "http://www.cpsc.gov/en/Recalls/2004/CPSC-NETGEAR-Inc-Announce-Recall-of-Wall-Plug-Ethernet-Bridges-/";
Document doc = Jsoup.connect(url).get();
Elements archived = doc.select("div.archived > *");
for(Element ele : archived) {
//what goes here to get those key/value pairs?
}
答案 0 :(得分:3)
这不是一个完整的答案,但它会让你95%。
String url="http://www.cpsc.gov/en/Recalls/2004/CPSC-NETGEAR-Inc-Announce-Recall-of-Wall-Plug-Ethernet-Bridges-/";
Document doc = Jsoup.connect(url).get();
Elements archived = doc.select("div.archived strong");
for (Element element: archived){
System.out.println("KEY: " + element.text());
System.out.println("VALUE: " + element.nextSibling());
}
输出:
KEY: Firm's Hotline: (800) 303-5507
VALUE: <br />
KEY: Name of product:
VALUE: Wall Plug Ethernet Bridge
KEY: Units:
VALUE: About 53,500 units
KEY: Manufacturer:
VALUE: NETGEAR Inc., of Santa Clara, Calif.
KEY: Hazard:
VALUE: The plastic housing on these units can detach, posing a shock hazard.
and so on...
正如你所看到的,它需要一些工作来忽略不必要的东西,比如第一个元素KEY / VALUE对和诸如此类的东西,但是否则它应该有效!祝你好运。