【发布时间】:2016-02-16 18:58:39
【问题描述】:
我有一个网站有源代码:
<article id="post-438" class="post-438 post type-post status-publish format-standard has-post-thumbnail hentry category-history tag-africa tag-asia tag-europe tag-maps tag-middle-east tag-mongol-empire tag-ottoman-empire tag-rise-of-islam">
<header class="entry-header">
<div class="entry-meta smallPart">
<span class="posted-on"><i class="fa fa-clock-o spaceRight"></i><a href="https://muslimmemo.com/map-rise-islam/" rel="bookmark"><time class="entry-date published updated" datetime="2015-07-23T00:26:22+00:00">July 23, 2015</time></a></span><span class="byline"> <i class="fa fa-user spaceLeftRight"></i><span class="author vcard"><a class="url fn n" href="https://muslimmemo.com/author/sufyan/">Sufyan bin Uzayr</a></span></span><span class="comments-link"><i class="fa fa-comments-o spaceLeftRight"></i><a href="https://muslimmemo.com/map-rise-islam/#respond"><span class="dsq-postid" data-dsqidentifier="438 https://muslimmemo.com/?p=438">Leave a comment</span></a></span> </div><!-- .entry-meta -->
<h1 class="entry-title"><a href="https://muslimmemo.com/map-rise-islam/" rel="bookmark">Map Showing The Rise of Islam Down The Ages</a></h1> </header><!-- .entry-header -->
<div class="entry-summary">
<p>This is a rather interesting map that shows the spread of Islam across Asia, Europe and Africa, down the ages. The earliest period is marked in shades of brown and red, followed by shades of yellow. South-east Asia is shown separately as an inset using shades of blue. While this map is far from perfect…</p>
</div><!-- .entry-summary -->
<footer class="entry-footer smallPart">
<div class="cruzy-bottom-content">
<span class="cat-links"><i class="fa fa-folder-open spaceRight"></i><a href="https://muslimmemo.com/content/history/" rel="category tag">History</a></span> <span class="read-link">
<a class="readMoreLink invertPart" href="https://muslimmemo.com/map-rise-islam/">Read More<i class="fa fa-angle-double-right spaceLeft"></i></a>
</span>
</div>
</footer><!-- .entry-footer -->
</article>
我需要获取:
- 我需要从网站获取的内容:
- 条目标题。 -> document.getElementByClassName("entry-title");
- entry-link -> document.select("span.entry-title > a[href]")
- 条目摘要。 -> document.getElementByClassName("entry-summary");
- 作者的链接。 -> document.select("span.author > a[href]")
- 作者姓名。 -> document.getElementByClassName("author");
- 类别。 -> document.getElementByClassName("cat-links");
- 类别的链接。 -> document.select("span.cat-links > a[href]")
- 发布日期 -> document.getElementsByClass("published");
我是这样做的:
Document document = Jsoup.connect(url).get();
heading = document.getElementsByClass("entry-title");
headingLink = document.select("h1.entry-title > a[href]");
headingSummary = document.getElementsByClass("entry-summary");
author = document.getElementsByClass("author");
authorLinks = document.select("span.author > a[href]");
category = document.getElementsByClass("cat-links");
categoryLinks = document.select("span.cat-links > a[href]");
published = document.getElementsByClass("published");
它运行良好,但运行速度很慢。我应该如何更改我的代码。请帮帮我。
【问题讨论】:
-
基本上,如果您使用 JSoup,您必须算上它的速度很慢。解析 html 并不是一项快速的任务。
-
@poss 还有其他选择吗?
-
向网站询问 API 似乎是一种最佳方法。 JSoup 有替代品,但它们可能不会做得更好。
-
我建议您使用 php 解析 web 中某处的 html 并将其转换为 json 提要。最后在您的应用程序中提取 json 并使用数据。这不会减慢您应用程序中的解析过程。相反,您的服务器将处理它。如果您发现此技术有用,我可以为您提供一些有用的链接。
-
您使用 JSoup 对我来说似乎没问题。至少我看不出有什么方法可以加快速度。一件事可能是通过不从文档级别开始,而是从合适的内部节点开始来限制对元素的搜索。 element.select(".whatever") 将从元素而不是文档开始。如果您的文档很大,这可能会有所帮助。
标签: java android html parsing jsoup