我正在为Android制作一个应用程序,应用程序的功能之一是返回图书馆目录在线搜索的结果。该应用程序需要以与其余应用程序相符的方式显示搜索结果,这是通过自定义HTML表单进行的。也就是说,需要解析搜索结果并显示有用的元素。我只是想知道在Android中是否可以实现这个功能?
Use the: Jsoup.connect(String url) method:
Document doc = Jsoup.connect("http://example.com/").get();
这将使您可以通过使用URL连接到HTML页面。并通过DOM将其存储为文档doc。然后使用selector()方法从中读取。
描述
connect(String url)方法创建一个新的Connection,并获取和解析HTML文件。如果在获取URL时出现错误,它将抛出IOException,您应该适当处理。
Connection接口旨在进行方法链接以构建特定请求:
Document doc = Jsoup.connect("http://example.com")
如果您仔细阅读Jsoup文档,应该可以实现此目标。
编辑:以下是使用selector方法的示例
//Once the Document is retrieved above, use these selector methods to Extract the data you want by using the tags, id, or css class
Elements links = doc.select("a[href]"); // a with href
Elements pngs = doc.select("img[src$=.png]");
// img with src ending .png
Element masthead = doc.select("div.masthead").first();
// div with class=masthead
Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
编辑:使用JSOUP,您可以使用以下代码获取属性、文本:
Document doc = Jsoup.connect("http://example.com")
Element link = doc.select("a").first();
String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""
String linkOuterH = link.outerHtml();
// "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html(); // "<b>example</b>"