java爬蟲——jsoup
一:所需知識
1.io操作
2.簡單學習框架jsoup
3.多線程
二:java文件介紹
1.Main.java --------------------程序入口
2.JsoupDemo.java-------------------爬蟲的邏輯部分
3.Movies.java--------------------javaBean類
4.MoviesDao.java----------------有關數據庫的操作
5.Bt_picture.java------------------電影圖片下載到本機
三:程序流程
1.通過鏈接分析,發現http://www.bttiantangs.com/list/dianying/index_2.html中的2代表頁碼,該首頁一共有500頁
2.每當爬取一頁時,從電影標題中獲取下一頁的鏈接
3.解析電影詳情頁中你所需要的信息。
四:程序代碼
Main.java:
import java.util.List;
public class Main {
public static void main(String [] args) throws Exception{
int k=0; //用來計數
MoviesDao md=new MoviesDao();//用來對獲取到的數據插入數據庫
Bt_picture p=new Bt_picture();//用來下載圖片
for(int i=2;i<50;i++){ //爬取地址的總數
String url="http://www.bttiantangs.com/list/dianying/index_"+i+".html";//爬取的地址
JsoupDemo jsoup=new JsoupDemo();
//獲取某一頁的所有電影詳情頁下的連接
List<String> href=jsoup.link(url);
List <Movies> m=jsoup.get(href);
for(Movies e:m){
k++;
//插入數據庫
p.download(e);
md.insert(e.getTitle(), e.getYear(), e.getContry(), e.getLan(), e.getDouban_link(), e.getIntroduce(), e.getMain_actor(), e.getDownload_url(), e.getImg_url());
System.out.println(k);
}
}
}
}
JsoupDemo.java
jsoup的使用都在這
package jsoup;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupDemo {
//獲取第二級頁面的所有鏈接地址並返回
public List<String> link(String url) throws Exception{
//由於獲取的連接只是一部分,所以要拼接
String link="http://www.bttiantangs.com";
List <String > href=new ArrayList<>();
Connection conn=Jsoup.connect(url).timeout(30000);
Document doc=conn.get();
//System.out.println(doc.html());
Elements ele=doc.select(".article >h2>a");
for(Element element:ele){
String a=element.attr("href");
//拼接完整的連接
String full=link+a;
href.add(full);
}
return href;
}
//根據第二級頁面的鏈接,將爬取到的電影集合返回
public List<Movies> get(List<String> href) throws IOException {
List<Movies> ls=new ArrayList<>();
for(String h:href){
Movies m=new Movies();
Connection conn=Jsoup.connect(h).timeout(30000);
Document doc;
try {
doc = conn.get();
} catch (IOException e) {
conn=Jsoup.connect("http://www.bttiantangs.com/movie/50680.html").timeout(30000);
doc = conn.get();
e.printStackTrace();
}
Elements ele_title=doc.select(".article_container >h1");//電影標題
m.setTitle(ele_title.get(0).text());
Elements ele_p=doc.select("#post_content :nth-child(2)");//電影其他信息
String p=ele_p.text();
String sum[]=p.split("◎");
for(String s:sum){
//年代截取
if(s.contains("年 代")){
m.setYear(s.substring(5, s.length()));
}
//國家截取
if(s.contains("國 家")){
m.setContry(s.substring(5, s.length()));
}
//語言截取
if(s.contains("語 言")){
m.setLan(s.substring(5, s.length()));
}
//豆瓣鏈接
if(s.contains("豆瓣鏈接")){
m.setDouban_link(s.substring(5, s.length()));
}
//主演
if(s.contains("主 演")){
m.setMain_actor(s.substring(5, s.length()));
}
}
//電影圖片
Elements ele_img=doc.select(".tpic-cont-s >img:nth-child(1)");
//有的不存在電影圖片
if(!ele_img.isEmpty()){
m.setImg_url(ele_img.attr("src"));
}
//電影簡介
Elements ele_introduce =doc.select(".minfos");
//有的不存在電影簡介
if(!ele_introduce.isEmpty()){
m.setIntroduce(ele_introduce.text());
}
//電影下載地址
Elements ele_download=doc.select(".dlist >li >a:nth-child(2)");
if(!ele_download.isEmpty()){
m.setDownload_url(ele_download.get(0).attr("href"));
}
ls.add(m);
}
return ls;
}
}
Movies.java
package jsoup;
public class Movies {
private String title;//電影標題
private String year;//電影年份
private String country;//國家
private String lan;//語言
private String douban_link;//豆瓣連接
private String introduce;//簡介
private String main_actor;//主演
private String download_url;//下載地址
private String img_url;//圖片下載地址
public Movies(){
title="null";
year="null";
country="null";
lan="null";
douban_link="null";
introduce="null";
main_actor="null";
download_url="null";
img_url="null";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getContry() {
return country;
}
public void setContry(String contry) {
this.country = contry;
}
public String getLan() {
return lan;
}
public void setLan(String lan) {
this.lan = lan;
}
public String getDouban_link() {
return douban_link;
}
public void setDouban_link(String douban_link) {
this.douban_link = douban_link;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public String getMain_actor() {
return main_actor;
}
public void setMain_actor(String main_actor) {
this.main_actor = main_actor;
}
public String getDownload_url() {
return download_url;
}
public void setDownload_url(String download_url) {
this.download_url = download_url;
}
public String getImg_url() {
return img_url;
}
public void setImg_url(String img_url) {
this.img_url = img_url;
}
}
MoviesDao.java
package jsoup;
import java.sql.Connection;
import java.sql.Statement;
public class MoviesDao {
private Util util;
private Connection conn;
private Statement st;
public MoviesDao() throws Exception{
util=new Util();
conn=util.conn();
st=conn.createStatement();
}
//將電影信息存入數據庫
public void insert(String title,String year,String country,String lan,String douban_link,String introduce,String main_actor,String download_url,String img_url ) throws Exception{
String sql="INSERT INTO movies VALUES('"+title.replace("'", "")+"','"+year.replace("'", "")+"','"+country.replace("'", "")+"','"+lan.replace("'", "")+"','"+douban_link.replace("'", "")+"','"+introduce.replace("'", "")+"','"+main_actor.replace("'", "").replaceAll(" ", "")+"','"+download_url.replace("'", "")+"','"+img_url.replace("'", "")+"');";
System.out.println(sql);
st.executeUpdate(sql);
}
}
Bt_picture.java
圖片下載
package jsoup;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
public class Bt_picture {
/**
*
* @param ls 傳遞多來的有關電影的信息,包括所需要的圖片地址,和電影名稱
* @throws IOException
*/
public void download(Movies e) throws IOException {
String url=e.getImg_url();
//圖片鏈接有的沒
if(url=="null"){
url="https://ws2.sinaimg.cn/large/6c7edb3fly1fguvf22hznj215o0k67h5.jpg";
}
Connection conn=Jsoup.connect(url);
Response rs=conn.ignoreContentType(true).timeout(3000).ignoreHttpErrors(true).execute();
//存放圖片的數組
byte b[]=rs.bodyAsBytes();
File file = new File("E:/BT_Movies_Picture",e.getTitle().replace(":", "")+".jpg");
if (!file.exists()) {
FileOutputStream raf = new FileOutputStream(file);
raf.write(b);
raf.close();
}
}
}
Util.java
數據庫的連接
package jsoup;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class Util {
//數據庫連接
public Connection conn() throws Exception{
// //創建Properties對象,用於加載配置信息
// Properties prop=new Properties();
// //將文件加載到字節輸入流中
// InputStream in=MoviesDao.class.getClassLoader().getResourceAsStream("db_properties.properties");
// //把流對象的數據放到prop對象中
// prop.load(in);
// String username=prop.getProperty("username");
// String password=prop.getProperty("password");
// String url=prop.getProperty("url");
// String driver=prop.getProperty("driver");
String username="root";
String password="zp1759999";
String url="jdbc:mysql://localhost:3306/bt_movies";
String driver="com.mysql.jdbc.Driver";
System.out.println(username);
//數據庫連接
//第一步:加載驅動
Class.forName(driver);
//第二步:建立數據庫連接
Connection conn=DriverManager.getConnection(url,username,password);
return conn;
}
}
五:總結
學習到的:
1.學會簡單的Jsoup 爬蟲
2.學會用jsoup下載網絡上的圖片等文件
3.熟練jdbc
4.熟練io操作
六:結果展示
電影圖片:
數據庫展示: