最近做java网页爬虫用jsoup 写了一个方法main执行正常 放在action里调用这个方法就不正常求解

发布网友 发布时间:2022-04-24 07:07

我来回答

1个回答

热心网友 时间:2023-10-08 23:20

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupTest {

static String url = "http://www.sogou.com/web?sut=1374&lkt=1%2C1386588673481%2C1386588673481&ie=utf8&sst0=1386588674552&p=40040100&dp=1&w=01019900&dr=1&_asf=www.sogou.com&_ast=13865056&query=java网页爬虫&page=1";

public static void main(String[] args) {
Document doc = readUrlFist(url);
write(doc);
}
public static void write(Document doc) {
try {

FileOutputStream fos=new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\a.html");
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
bw.write(doc.toString());
bw.flush();
fos.close();
osw.close();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}

}
public static Document readUrlFist(String url) {
Document doc = null;
Connection conn = Jsoup.connect(url);
conn.header(
"User-Agent",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 Googlebot/2.1");
try {
doc = conn.timeout(200 * 1000).get();
} catch (IOException e) {
e.printStackTrace();
if ((e instanceof UnknownHostException)
|| (e instanceof SocketTimeoutException)) {
doc = readUrlFist(url);
}

}
return doc;
}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com