这里以阿里的招聘为例:
静态页面主要是注意其页面的结构,使用F12可以很容易的就找到,之后使用jousp解析这个页面,转换成dom树,思路很简单接下来代码了:
package com.learn;
import java.util.List;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class PaTencet {
public static void main(String[] args){
try {
login();
} catch (Exception e) {
// TODO Auto-generated catch block
e.getMessage();
}
}
public static void login() throws Exception {
Connection con2 = Jsoup
.connect("https://campus.alibaba.com/positionList.htm");
con2.header("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
Response rs = con2.execute();
// 转换为Dom树
Document d1 = Jsoup.parse(rs.body());
// 获取td,可以通过查看页面源码代码得知
Element eone = d1.select("#J-filter").get(0);
// 获取tbody,可以通过查看页面源码代码得知
List<Element> et = d1.select("tbody[class=campus-position-table-body]");
// 获取,cooking和表单属性,下面map存放post时的数据
for (Element e : et.get(0).getAllElements()) {
if (e.attr("data-type").equals("11")) {
//System.out.println("DD"+e.html());
String url =e.select("td[class = position-detail] a[href]").attr("href");
String title =e.select("th").text();
String city =e.select("td[class =work-city]").text();
String experience ="请看详情";
String salary ="请看详情";
String education ="请看详情";
String publictime =e.select("td").get(2).html();
String details = url;
String neednum ="请看详情";
String company ="阿里巴巴";
String jobtype =eone.select("a[data-type=11]").text();
try {
String sql ="insert into jobtable(url,title,city,experience,salary,education,publictime,details,neednum,company,jobtype) values('"+url+"','"+title+"','"+city+"','"+experience+"','"+salary+"','"+education+"','"+publictime+"','"+details+"','"+neednum+"','"+company+"','"+jobtype+"')";
int r =mysqlUtil.add(sql);
if (r==1) {
System.out.println("插入成功");
}else {
System.out.println("失败");
}
} catch (Exception e2) {
// TODO: handle exception
throw new Exception("login()方法的foreach循环的第插入失败了,在e.attr(\"data-typ