Java爬虫 爬取静态页面(不使用ajax)

本文通过实例讲解了如何使用Java爬虫抓取静态页面,以阿里巴巴招聘页面为例,通过分析页面结构,利用jsoup解析并转换为DOM树。
摘要由CSDN通过智能技术生成

这里以阿里的招聘为例:
静态页面主要是注意其页面的结构,使用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值