Jsoup,从网站提取链接,图像.运行时异常 [英] Jsoup, extract links, images, from website. Exception on runtime
问题描述
我刚刚开始使用Jsoup,所以请遵循他们在该网站上的教程.我认为这段代码应该可以正常工作,但是当我在netbeans上运行它时,我会遇到错误.
I am just starting to play around with Jsoup, so followed the tutorial they had on there website. This code i assume should work fine but when i am running it on netbeans I am coming across errors.
这是我正在使用的代码:
This is the code i am using:
`
/**
*
* @author Slabs One
*/
public class ParseWebpage {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "http://www.gumtree.com.au/");
String url = args[0];
print("Fetching %s...", url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
Elements media = doc.select("[src]");
Elements imports = doc.select("link[href]");
print("\nMedia: (%d)", media.size());
for (Element src : media) {
if (src.tagName().equals("img"))
print(" * %s: <%s> %sx%s (%s)",
src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
trim(src.attr("alt"), 20));
else
print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
}
print("\nImports: (%d)", imports.size());
for (Element link : imports) {
print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
}
print("\nLinks: (%d)", links.size());
for (Element link : links) {
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
}
private static void print(String msg, Object... args) {
System.out.println(String.format(msg, args));
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
else
return s;
}
}
`
这段代码应该不会有任何问题,但是在运行时我会收到此错误:
There shouldnt be any problems with this code, but on runtime i get this error:
Exception in thread "main" java.lang.IllegalArgumentException: http://www.gumtree.com.au/
at org.jsoup.helper.Validate.isTrue(Validate.java:45)
at parsewebpage.ParseWebpage.main(ParseWebpage.java:25)
任何对为什么不能正常工作的见解将不胜感激
Any insight to why this is not working would be greatly appreciated
推荐答案
要在Netbeans
中传递命令行参数,您必须执行以下操作.
To pass command line arguments in Netbeans
, You have to do the following.
- 右键单击项目,然后单击属性
- 在侧窗格中选择 Run
- 使用浏览对话框选择您的主类(ParseWebpage)
- 输入参数(URL)
- 单击确定
- Right-click the project and click properties
- Choose Run in the side pane
- Choose your Main class (ParseWebpage) using Browse dialog
- Input the Arguments (URL)
- Click ok
现在,如果您运行Main类(ParseWebpage),则运行时参数将在NetBeans IDE中传递
Now, if you run the Main class (ParseWebpage), the run time argument will be passed in NetBeans IDE
这篇关于Jsoup,从网站提取链接,图像.运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!