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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Qt开发笔记之QCustomPlot:QCustomPlot介绍、编译与使用

    Qt开发笔记之QCustomPlot:QCustomPlot介绍、编译与使用欢迎技术交流和帮助,提供所有IT相关的服务,有需要请联系博主QQ:21497936,若该文为原创文章,未经允许不得转载原博主博客地址:http://blog.csdn.net/qq21497936本文章博客地址:http://blog.csdn.net/qq21497936/article/details/77847820目录效果​Demo下载地址QCustom…

    2022年10月17日
    0
  • Python fillna_pandas fillna 指定列

    Python fillna_pandas fillna 指定列对我来说工作:df.ix[df[‘Type’]==’Dog’,’Killed’]=df.ix[df[‘Type’]==’Dog’,’Killed’].fillna(2.25)print(df)TypeKilledSurvived0Dog5.0021Dog3.0042Cat1.0073Dog2.2534cowNaN2如果系列需要fillna–因…

    2022年8月12日
    13
  • luogu1146

    luogu1146#include#definefr(i,a,b)for(inti=(a),i_end=(b);i<=i_end;i++)#definefrd(i,a,b)for(inti=(a),i_end=(b);i>=i_end;i–)#definelllonglong#definepri(x)printf(“%d”,x)#definemes(x

    2022年5月16日
    43
  • 「机械工程」力矩,转矩,扭矩的理解

    「机械工程」力矩,转矩,扭矩的理解1.力矩定义:力矩在物理学里是指作用力使物体绕着转动轴或支点转动的趋向。力矩的单位是牛顿-米。力矩希腊字母是tau。力矩的概念,起源于阿基米德对杠杆的研究。转动力矩又称为转矩或扭矩。力矩能够使物体改变其旋转运动。推挤或拖拉涉及到作用力,而扭转则涉及到力矩。力矩等于径向矢量与作用力的叉积。力矩在物理学里是指作用力使物体绕着转动轴或支点转动的趋向,是力对物体产生转动作用的物理量。可以分…

    2022年5月15日
    72
  • java基础入门(一)[通俗易懂]

    前言:1.笔者的java没有经过真正系统的学习过,只是跟着书上自学的。所以有些地方难免会理解错误之类的,如果看到错误的地方,请指出来,或者有什么不理解的地方也可以提出来,大家一起进步。2.这篇教程是一个学习方向的引导,且只针对基础入门(更加进阶的知识笔者也还在学习)。3.java的基础入门知识网上有很多,很多大神的博客里也有总结,笔者不认为自己能比大神总结的好。所以在这篇教程里,…

    2022年4月6日
    36
  • PHP SPL他们留下的宝石

    PHP SPL他们留下的宝石

    2022年1月3日
    43

发表回复

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

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