我试图构建一个从网站收集HTML源代码的爬虫,这是我在一个.csv文件中拥有的。每当我把链接放进去时,一切似乎都很好。
url = new URL ("http://example.com")但是每当我试图将链接放入变量(本例中为“text”)时,我就会得到一个错误,告诉我已经有了一个malformedURLException。
这是我的代码:
String text ="http://stackoverflow.com/questions/9827143/continuing-execution-after-an-exception-is-thrown-in-java";
// get the sourcecode of the link you just grabbed
url = new URL(text);
PrintWriter writer = new PrintWriter("sourcecode.txt", "UTF-8");发布于 2015-06-01 20:40:09
你的双引号有问题。
我将您的"text“行粘贴到Eclipse并试图保存,它向我展示了"text”字符串开头有一个无效字符,因为有一个Cp1252编码字符。
我删除了你的第一个双引号,然后重新输入。然后我跑了
String text = "http://stackoverflow.com/questions/9827143/continuing-execution-after-an-exception-is-thrown-in-java";
try {
URL url = new URL(text);
PrintWriter writer = new PrintWriter("sourcecode.txt", "UTF-8");
System.out.println("all good");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}而且起作用了。
发布于 2015-06-01 20:43:49
文本变量中有一个特殊的字符。刚刚在浏览器中试了一下你的链接,但由于这个原因,它没有工作。
复制以下内容,再试一次:
String text ="http://stackoverflow.com/questions/9827143/continuing-execution-after-an-exception-is-thrown-in-java";https://stackoverflow.com/questions/30582751
复制相似问题