Java爬取豆瓣电影Top250并使用Mybatis框架存储数据

结果部分截图如下

 我使用的是 IntelliJ IDEA ,首先先建立一个普通的maven项目

先创建一个实体类来存储电影信息


/**
 * 存储页面信息实体类
 */
public class Page {
    //id
    private int id;
    //评论数
    private String commentcount;
    //电影名称
    private String title;
    //电影介绍
    private String jieshao;
    //电影评分
    private String fenshu;
    
    //。。。。。此处省略getter和setter
}

创建一个通过URL来获取网页html的工具类PageDownLoadUtil,用来把网页内容下载下来

package util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class PageDownLoadUtil {

    public static String getPageContent(String url) {
        HttpClientBuilder builder = HttpClients.custom();
        CloseableHttpClient client = builder.build();

        HttpGet request = new HttpGet(url);
        String content = null;
        try {
            CloseableHttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();

            content = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return content;
    }


}

做一个提取标签的工具类 htmlUtil

package util;

import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public class htmlUtil {
    /**
     * 获取标签属性值
     * @param tagNode
     * @param xpath
     * @param att   匹配标签值
     * @return
     */
    public static String getAttributeByName(TagNode 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值