Java爬取网页数据HTML,CSS,JS

最近接触了下java的爬虫,文本信息爬完了,就想看看图片怎么爬,于是就研究了一下,案例爬学校的官网pom依赖<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> <groupId>org.jsoup</groupId>...
摘要由CSDN通过智能技术生成

最近接触了下java的爬虫,文本信息爬完了,就想看看图片怎么爬,于是就研究了一下,案例爬学校的官网

pom依赖

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

        <!-- 文件下载 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>

设置配置信息:

public class test {
   
    // 地址
    private static final String URL = "http://www.ktbdqn.com/";
    // 编码
    private static final String ECODING = "GBK";
    // 获取img标签正则
    private static final String IMGURL_REG = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
    //获取link标签正则
    private static final String LINKURL_REG = "<link.*href\\s*=\\s*(.*?)[^>]*?>";
    // 获取Img的src路径的正则
    private static final String IMGSRC_REG = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
    // 获取Link的href路径的正则
    private static final String LINKSRC_REG = "(?x)(href|HREF)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(css|CSS|([\\w-]+/)*([\\w-]+\\.(css|CSS|([\\w-]+/)*([\\w-]+\\.(css|CSS)))))))('|\")";
    // css本地保存路径
    private static final String SAVE_CSS_PATH = "d:\\cskt\\";
    // img本地保存路径
    private static final String SAVE_PATH = "d:\\";

这里注意有些人获取的网页会乱码,根据爬取的网页编码和自己的编码保持一致

图片下载

public static void Download(List<String> listImgSrc) {
   
        int count = 0;
        try {
   
            for (int i = 0; i < listImgSrc.size(); i++) {
   
                String url = listImgSrc.get(i);
                String imageName = url.substring(url.lastIndexOf("/") + 1, url.length());
                URL uri = new URL(url);
                // 打开连接
                URLConnection con = uri.openConnection();
                //设置请求超时为5s
   
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
jsoup是一个JavaHTML解析器,它可以很方便地解析HTML文档,获取HTML中的元素、属性、文本等信息。但是,jsoup并不支持解析JavaScript和CSS,因为JavaScript和CSS是在浏览器中解析的,而不是在HTML中解析的。 如果你想爬取页面的JavaScript和CSS,可以使用其他工具来获取页面源代码,如HttpClient、Selenium等。这些工具可以模拟浏览器的行为,获取完整的页面源代码,包括JavaScript和CSS。 下面是一个使用HttpClient和Jsoup来爬取页面的例子: ```java import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.IOException; public class JsoupDemo { public static void main(String[] args) throws IOException { String url = "http://www.example.com"; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); String html = ""; try (CloseableHttpResponse response = httpClient.execute(httpGet); InputStream inputStream = response.getEntity().getContent()) { html = IOUtils.toString(inputStream, StandardCharsets.UTF_8); } Document document = Jsoup.parse(html); // 获取页面中的JavaScript和CSS Elements scripts = document.select("script"); Elements styles = document.select("style"); // 处理页面元素 // ... httpClient.close(); } } ``` 这个例子中,我们使用HttpClient来发送HTTP请求获取页面源代码,然后使用Jsoup来解析页面元素。通过`document.select("script")`和`document.select("style")`来获取页面中的JavaScript和CSS
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值