我正在尝试使用Jsoup从Uefa website中删除图片网址,但我遇到了错误。这是我用它来检索图像网址的代码。
Document doc = Jsoup.connect(Utility.url).get();
Elements img = doc.select("img");
我使用Breakpoints来查看哪一行不起作用,我发现doc变量正常工作以获取所有页面,但是当我调试下一行时,我得到null和一个带有0个对象的数组。我已经查看了stackoverflow,但我找不到解决问题的方法所以我尝试了Elements img = doc.title();
并且工作得很好。请让我知道我做错了什么。
<head></head>
<body onload="SubscribeHandler.writeContent();" onunload="SubscribeHandler.uninit();">
<div id="feedHeaderContainer"></div>
<script type="application/javascript"></script>
<div id="feedBody">
<div id="feedTitle"></div>
<div id="feedContent">
<div class="entry">
<h3></h3>
<div class="feedEntryContent" base="http://www.uefa.com/rssfeed/news/rss.xml"></div>
<div class="enclosures">
Media files
<div class="enclosure">
<img class="type-icon" src="moz-icon://.txt?size=16"></img>
<a href="http://www.uefa.com/MultimediaFiles/Photo/competitions/Comp_Matches/01/81/52/90/1815290_s5.jpg"></a>
(2.0 KB)
</div>
</div>
</div>
<div style="clear: both;"></div>
<div class="entry"></div>
<div style="clear: both;"></div>
<div class="entry"></div>
答案 0 :(得分:1)
我认为你可以使用HTML解析器从html数据中获取价值。这里很好SO answer。通过它
答案 1 :(得分:0)
有几个地方你错了: 1.您想要解析XML RSS提要。要解析XML RSS feed,您需要使用JSoup的“xmlParser”。
我附上下面的代码来提取图片网址。这段代码已经过我的测试。如有任何问题,请告诉我。
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
public class main {
public static void main(String[] args) throws IOException {
String url = "http://www.uefa.com/rssfeed/news/rss.xml";
Document doc = Jsoup.connect(url).parser(Parser.xmlParser()).ignoreContentType(true).get();
for (Element x : doc.getElementsByTag("enclosure")) {
System.out.println(x.attr("url"));
}
}
}