1、新建Java Project
2、右键项目------->Configure--------->Convert to Maven Project,直接点击finish
3、在生成的pom.xml中配置如下内容:
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
</dependencies>
4、创建java,编写爬虫
a. 爬取知乎中新闻的标题和内容
package com.spider;
import java.io.IOException;
import java.sql.SQLException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ZhihuJsoup {
public static void main(String[] args) throws SQLException {
String url = "http://zhihu.sogou.com/";
acquireDate(url);
}
/**
* 这是一个方法,需要传入一个字符串
*
* @param url
* @throws SQLException
*/
public static void acquireDate(String url) throws SQLException {
try {
Document doc = Jsoup.connect(url).get(); //获取url代表的网页的源代码
Elements element = doc.getElementsByClass("tit"); // 获取网页里面class为tit的代码集合
Elements ahref= element.select("a[href]"); // 获取网页上面那段代码中的的包含href属性的a标签,得到一个集合
// 遍历集合
int i = 0;
for (Element ele : ahref) { //数据太多,限制了5次
if(i==5){
break;
}
String href = ele.attr("href"); //获取得到的第一个子链接的url
Document docc = Jsoup.connect(href).get(); //获取url代表的网页的源代码
// 我使用的方法很简单,例如下面:div.inner-left>h1 意思就是说有个标签叫div,这个div
// 的class是inner-left,">"这个符号的意思是这个标签里面有个h1的标签,".text"这个意思是把这个h1标签里面的文字转换出来变成String字符串
System.out.println("第"+ (++i) + "条数据");
System.out.println("标题图片 : " + docc.select("#root > div > main > div > img").attr("src")); //通过右键---->copy---->copy seletor获得的
//System.out.println("标题图片 : " + docc.select("img[class=TitleImage]").attr("src")); //通过img标签中class = TitleImage获得
//System.out.println("标题图片 : " + docc.select(".TitleImage").attr("src")); //通过class直接获得
System.out.println("标题 : " + docc.select("#root > div > main > div > article > header > h1").text());
System.out.println("头像 : " + docc.getElementsByClass("Avatar Avatar--round AuthorInfo-avatar").attr("src"));
System.out.println("作者 : " + docc.getElementsByClass("UserLink-link").text());
System.out.println("作者简介 : " + docc.select("#root > div > main > div > article > header > div.Post-Author > div > div > div.AuthorInfo-detail > div > div").text());
System.out.println(""+docc.select("#root > div > main > div > article > div:nth-child(2) > div"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
b. 爬取小说的内容并下载
package com.spider;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Novel {
public static void main(String[] args) {
String url = "http://www.biquge5200.com/31_31746/12331189.html";
File file = new File("D:\\novel.txt"); //定义下载文件的存放路径
FileWriter fw = null; //常见FileWriter对象
try {
fw = new FileWriter(file);
} catch (IOException e) {
e.printStackTrace();
}
int i =0;
while(i<3) { //根据小说的章节设置数值
Document document = getDoc(url);
String title = document.select("#wrapper > div.content_read > div > div.bookname > h1").text(); //小说每章的标题
String text = document.select("#content").text(); //小说每章的内容
Elements nextdiv = document.select(".bottem2"); //下一章所在的div的class值
String nextdivstr = nextdiv.toString(); //转化未字符串
String[] nexturl = nextdivstr.split("下一章"); //截取下一章之前的代码
nextdivstr = nexturl[0]; //获得值
nexturl = nextdivstr.split("\u2192"); //->右箭头为2192
nextdivstr = nexturl[1]; //获取下一章的链接
url = nextdivstr.substring(12, nextdivstr.length()-2); //获得url
try { //写入标题和内容
fw.write(title);
fw.write(text);
} catch (IOException e) {
e.printStackTrace();
}
try { //清除缓存
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
i++;
System.out.println("第"+i+"章已完成");
}
try { //下载完成后,关闭流
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Document getDoc(String url)
{
boolean flag = false;
Document document = null;
do{
try {
document = Jsoup
.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
.timeout(5000)
.get();
flag = false;
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
flag = true;
}
}while(flag);
return document;
}
}
执行完成后,就可以在D:\novel.txt中观看小说了。