Java爬取先知论坛文章

Java爬取先知论坛文章0x00前言上篇文章写了部分爬虫代码,这里给出一个完整的爬取先知论坛文章代码,用于技术交流。0x01代码实现pom.xml加入依赖:<dependencie

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

Java爬取先知论坛文章

0x00 前言

上篇文章写了部分爬虫代码,这里给出一个完整的爬取先知论坛文章代码,用于技术交流。

0x01 代码实现

pom.xml加入依赖:

<dependencies>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>





    </dependencies>

实现代码

实现类:

package xianzhi;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Climbimpl implements Runnable {
    private String url ;
    private int pages;
    private String filename;



    Lock lock = new ReentrantLock();

    public Climbimpl(String url, int pages,String filename) {
        this.url = url;
        this.pages = pages;
        this.filename = filename;
    }

    public void run() {
        File file = new File(this.filename);

        boolean mkdir = file.mkdir();

        if (mkdir){
            System.out.println("目录已创建");
        }

        lock.lock();

//        String url = "https://xz.aliyun.com/";

        for (int i = 1; i < this.pages; i++) {
            try {

            String requesturl = this.url+"?page="+i;
            Document doc = null;
            doc = Jsoup.parse(new URL(requesturl), 10000);
            Elements element = doc.getElementsByClass("topic-title");
            List<String> href = element.eachAttr("href");
                for (String s : href) {
                    try{
                        Document requests = Jsoup.parse(new URL(this.url+s), 100000);
//                        String topic_content = requests.getElementById("topic_content").text();
                        String titile = requests.getElementsByClass("content-title").first().text();
                        System.out.println("已爬取"+titile+"->"+this.filename+titile+".html");


                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(this.filename+titile+".html"));
                        bufferedOutputStream.write(requests.toString().getBytes());
                        bufferedOutputStream.flush();
                        bufferedOutputStream.close();


                    }catch (Exception e){
                        System.out.println("爬取"+this.url+s+"报错"+"报错信息"+e);
                    }
                }


            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        lock.unlock();

    }
}


main类:

package xianzhi;

public class TestClimb {
    public static void main(String[] args) {
        int Threadlist_num = 10; //线程数
        String url = "https://xz.aliyun.com/";  //设置url
        int pages = 10; //读取页数
        String path = "D:\\paramss\\";  //设置保存路径

        Climbimpl climbimpl = new Climbimpl(url,pages,path);
        for (int i = 0; i < Threadlist_num; i++) {
            new Thread(climbimpl).start();

        }
    }
}

Java爬取先知论坛文章

0x03 结尾

该爬虫总体的代码都比较简单。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119947.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月12日 上午7:00
下一篇 2021年12月12日 上午7:00


相关推荐

  • Android之ViewPager的简单使用[通俗易懂]

    Android之ViewPager的简单使用[通俗易懂]AndroidViewPager的简单使用Android3.0后引入的一个UI控件——ViewPager(视图滑动切换工具)功能:通过手势滑动可以完成View的切换,一般是用来做APP的引导页或者实现图片轮播,因为是3.0后引入的,如果想在低版本下使用,就需要引入v4兼容包ViewPager就是一个简单的页面切换组件,我们可以往里面填充多个View,然后我们可以左右滑动,从而切换不同的View,我们可以通过setPageTransformer()方法为我们的ViewPager设置切换时

    2022年7月22日
    8
  • Python画图

    Python画图帮别人实现rubberfriction的运算公式。公式不是我专业的内容,完全不懂,只是照着公式用python计算并画图。做出的图,与论文中的图进行对比,完全一致。之所以用python,是因为mat

    2022年7月5日
    26
  • h5 Python_python做h5网站

    h5 Python_python做h5网站(一)HDF与h5  HDF(HierarchicalDataFormat层次数据格式)是一种设计用于存储和组织大量数据的文件格式,最开始由美国国家超算中心研发,后来由一个非盈利组织HDFGroup支持。HDF支持多种商业及非商业的软件平台,包括MATLAB、Java、Python、R和Julia等等,现在也提供了Spark。其版本包括了HDF4和现在大量用的HDF5。h5是HDF5文件格…

    2025年10月17日
    5
  • 惊艳!用即梦3.0轻松打造酷炫字体海报(附提示词+超级公式)

    惊艳!用即梦3.0轻松打造酷炫字体海报(附提示词+超级公式)

    2026年3月13日
    2
  • 探索n8n自動化平台:從新手到專業的完整指南

    探索n8n自動化平台:從新手到專業的完整指南

    2026年3月15日
    2
  • linux struts2漏洞,Struts2漏洞分析,漏洞波及全系版本

    linux struts2漏洞,Struts2漏洞分析,漏洞波及全系版本Struts漏洞分析ApacheStruts团队已经发布了Struts2.3.15.1安全更新版本。在Struts2.3.15.1版本之前,存在着严重的安全漏洞,如果现在一些比较大的网站是用JAVA做的,没有把版本升级,还用的是Strtus2.3.15.1版本之前的话,那么你们就要小心,服务器被黑了哦。下面就来说一下之前版本,Struts2的漏洞是如何产生的,我们自己去做,该如何的去解决这个…

    2022年7月19日
    15

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号