java jsoup 爬虫 怎么防止重复爬取
3个回答
展开全部
方法/步骤 本次经验是通过导入外部Jar进行对网页数据进行抓取,以下是我工程的分布图。 本例子中是使用Jquery进行处理页面事件 页面展示 后台是在siteproxy.jsp中进行处理 5 最后将这其中需要的几个文件部署在此工程中去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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=1386589056&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;
}
}
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=1386589056&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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询