jsoup分页2

首先用JDBC建立数据库连接 需要导入Connection包

package jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class jdbc {
// 创建静态全局变量
static Connection conn;
static Statement st;


public static void insert(String type, String zy, String title) {
conn = getConnection(); // 首先要获取连接,即连接到数据库
try {
String sql = "INSERT INTO `txt` (`id`,`type`, `zy`, `titel`)"
+ " VALUES (CONCAT('snake_ZhongGuoUnvGeology_',UUID_SHORT()),'" + type + "', '" + zy + "', '"
+ title + "')"; // 插入数据的sql语句
st = (Statement) conn.createStatement(); // 创建用于执行静态sql语句的Statement对象
int count = st.executeUpdate(sql); // 执行插入操作的sql语句,并返回插入数据的个数
conn.close(); // 关闭数据库连接


} catch (SQLException e) {
System.out.println("插入数据失败" + e.getMessage());
}
}


/* 获取数据库连接的函数 */
public static Connection getConnection() {
Connection con = null; // 创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动


con = DriverManager.getConnection("jdbc:mysql://localhost:3306/air", "root", "root");// 创建数据连接


} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; // 返回所建立的数据库连接
}

}

测试类  


package collect;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.swing.JEditorPane;


import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import jdbc.jdbc;


public class FenYerequest {


@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
String url = "http://ir.shu.edu.cn/SearchResult/ResultListPage";
Map map=new HashMap<>();
map.put("CurrentBibType[0][Value]", "3");
map.put("CurrentBibType[0][Id]", "3");
map.put("CurrentBibType[0][Name]", "期刊论文");
map.put("CurrentOrder", "Year");
map.put("PageSize", "10");
map.put("PageIndex", "1");
Connection connection = Jsoup.connect(url).method(Method.POST).data(map);
Document document=connection.post();
Elements elements=document.select("#syspagebar a");
String str=elements.text();

System.out.println(str);
String[] a=str.split(" ");
boolean boo=a[a.length-1].equals(">>");
int pageSum=0;
if(boo==true){
pageSum=Integer.parseInt(a[a.length-2]);
System.out.println(pageSum);
}else {
pageSum=Integer.parseInt(a[a.length-1]);
System.out.println(pageSum);
}
for (int pageNo = 3; pageNo < 5; pageNo++) {
System.out.println("第----------"+pageNo+"--------------页     开始");
getData(url,pageNo+"");
System.out.println("第----------"+pageNo+"--------------页      结束");


}
}



@SuppressWarnings("unchecked")
public static void getData(String url,String num) throws IOException{
Map map=new HashMap<>();
map.put("CurrentBibType[0][Value]", "3");
map.put("CurrentBibType[0][Id]", "3");
map.put("CurrentBibType[0][Name]", "期刊论文");
map.put("CurrentOrder", "Year");
map.put("PageSize", "10");
map.put("PageIndex", num);
Connection connection = Jsoup.connect(url).method(Method.POST).data(map);
Document document=connection.post();
Elements elements1=document.select("#searchresult #list ul>li");
for (Element element : elements1) {
//String titel1=element.select(".subtitler a").text();//文章标题
String link1=element.select(".subtitler a").attr("abs:href").trim();
getData2(link1);
}
}
@SuppressWarnings("unchecked")
public static void getData2(String url) throws IOException{
String type="";
String zy="";
Document document=Jsoup.connect(url).get();
Elements elements=document.select("#literature_details>div[class=literature_box]");
String titel=document.select(".literature_cn").text().trim();//文章标题
//System.out.println("新闻标题:"+titel);
List<String> list=new ArrayList<String>();
Map<String, String> map=new HashMap<String,String>();
//把获取的字符串添加到list集合中
for (Element element : elements){
String text=element.text().replace("  ", ";");
list.add(text);
}
//遍历集合 将每个字符串拆分 成k_v格式存到map集合中
for (String str : list) {
String[] str2=str.split(":");
map.put(str2[0], str2[1]);
}
for (String k : map.keySet()) {
if(k.equals("文献类型")){
type=map.get(k);
}


}
if(!document.select(".literature_zy").isEmpty()){
String zhaiyao=document.select(".literature_zy").text().trim();//摘要
String[] strings=zhaiyao.split(":");
zy=strings[1];
}
jdbc jdbc=new jdbc();
jdbc.insert(type, zy, titel);
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值