在 Java 中查找 href 链接和 URL 的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10300588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:27:20 来源:igfitidea点击:
Find content of href link and URL in Java
提问by Thordax
I want to parse this link :
我想解析这个链接:
<a href="http://www.google.fr">Link to google</a>
In order to get two results:
为了得到两个结果:
Link = "http://www.google.fr"
LinkName = "Link to google"
I really don't know how to do this, is there a library in Java to solve this problem ?
我真的不知道该怎么做,Java 中是否有一个库可以解决这个问题?
Thanks in advance,
提前致谢,
采纳答案by Bitmap
This will do.
这会做。
public class Parse
{
public static void main(String[] args)
{
String h = " <a href=\"http://www.google.fr\">Link to google</a>";
int n = getIndexOf(h, '"', 0);
String[] a = h.substring(n).split(">");
String url = a[0].replaceAll("\"", "");
String value = a[1].replaceAll("</a", "");
System.out.println(url + " - " + value);
}
public static int getIndexOf(String str, char c, int n)
{
int pos = str.indexOf(c, 0);
while (n-- > 0 && pos != -1)
{
pos = str.indexOf(c, pos + 1);
}
return pos;
}
}
回答by Nurlan
Use jsoupparser:
使用jsoup解析器:
example:
例子:
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}