从HTML提取链接 [英] Extracting links from HTML
本文介绍了从HTML提取链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从HTML中提取链接.我正在使用以下正则表达式
I am trying to extract links from HTML. I am using the following regular expression
href=\"([^\"]*)\"
哪个正在提取不必要的链接.如何编写正则表达式以仅提取具有class ="l"之类的链接
Which is extracting unnecessary links. How can I write a regular expression to extract only links with class="l" like
<a href="http://users.elite.net/runner/jennifers/hello.htm" class="l">
<a href="http://www.hellodesign.com/" class="l">
<a href="http://www.ipl.org/div/hello/" class="l">
推荐答案
使用正则表达式解析HTML不必要地过于复杂.正则表达式是这项工作的错误工具.只需使用普通的HTML解析器,例如 Jsoup .它允许您通过常规 CSS选择器来选择HTML元素.
Parsing HTML with regex is unnecessarily overcomplicated. Regex is the wrong tool for the job. Just use a normal HTML parser like Jsoup. It allows you to select HTML elements by normal CSS selectors.
Document document = Jsoup.parse(html);
Elements links = document.select("a.l"); // Select all <a class="l"> elements.
for (Element link : links) {
System.out.println(link.absUrl("href"));
}
这篇关于从HTML提取链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文