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(); }
}
}

0x03 结尾

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

Java爬取先知论坛文章的更多相关文章

  1. Java爬取校内论坛新帖

    Java爬取校内论坛新帖 为了保持消息灵通,博主没事会上上校内论坛看看新帖,作为爬虫爱好者,博主萌生了写个爬虫自动下载的想法. 嗯,这次就选Java. 第三方库准备 Jsoup Jsoup是一款比较好 ...

  2. 信息领域热词分析系统--java爬取CSDN中文章标题即链接

    package zuoye1; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLExce ...

  3. Java爬取网络博客文章

    前言 近期本人在某云上购买了个人域名,本想着以后购买与服务器搭建自己的个人网站,由于需要筹备的太多,暂时先搁置了,想着先借用GitHub Pages搭建一个静态的站,搭建的过程其实也曲折,主要是域名地 ...

  4. MinerHtmlThread.java 爬取页面线程

    MinerHtmlThread.java 爬取页面线程 package com.iteye.injavawetrust.miner; import org.apache.commons.logging ...

  5. MinerConfig.java 爬取配置类

    MinerConfig.java 爬取配置类 package com.iteye.injavawetrust.miner; import java.util.List; /** * 爬取配置类 * @ ...

  6. Java爬取B站弹幕 —— Python云图Wordcloud生成弹幕词云

    一 . Java爬取B站弹幕 弹幕的存储位置 如何通过B站视频AV号找到弹幕对应的xml文件号 首先爬取视频网页,将对应视频网页源码获得 就可以找到该视频的av号aid=8678034 还有弹幕序号, ...

  7. java爬取网页内容 简单例子(2)——附jsoup的select用法详解

    [背景] 在上一篇博文java爬取网页内容 简单例子(1)——使用正则表达式 里面,介绍了如何使用正则表达式去解析网页的内容,虽然该正则表达式比较通用,但繁琐,代码量多,现实中想要想出一条简单的正则表 ...

  8. 【Python】爬取理想论坛单帖爬虫

    代码: # 单帖爬虫,用于爬取理想论坛帖子得到发帖人,发帖时间和回帖时间,url例子见main函数 from bs4 import BeautifulSoup import requests impo ...

  9. java爬取并下载酷狗TOP500歌曲

    是这样的,之前买车送的垃圾记录仪不能用了,这两天狠心买了好点的记录仪,带导航.音乐.蓝牙.4G等功能,寻思,既然有这些功能就利用起来,用4G听歌有点奢侈,就准备去酷狗下点歌听,居然都是需要办会员才能下 ...

随机推荐

  1. JavaScript动画实例:沿五角星形线摆动的小圆

    五角星形线的笛卡尔坐标方程式可设为: r=10+(3*sin(θ*2.5))^2  x=r*cos(θ) y=r*sin(θ)              (0≤θ≤2π) 根据这个曲线方程,在[0,2 ...

  2. C#中的char和string的使用简介

    char 字符 char代表一个Unicode字符,它是System.Char的别名 char someChar = 'a';//定义了一个字符 char newLine= '\n';//这是一个换行 ...

  3. Python数据可视化基础讲解

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:爱数据学习社 首先,要知道我们用哪些库来画图? matplotlib ...

  4. 题解 洛谷 P3185 【[HNOI2007]分裂游戏】

    首先可以发现,当所有巧克力豆在最后一个瓶子中时,就无法再操作了,此时为必败状态. 注意到,对于每个瓶子里的巧克力豆,是可以在模\(2\)的意义下去考虑的,因为后手可以模仿先手的操作,所以就将巧克力豆个 ...

  5. 洛谷p1120小木棍(剪枝优化)

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  6. hostapd阅读(openwrt)-3

    从官网下载相对而言比较干净的源码版本http://w1.fi/hostapd/,然后将其移植到openwrt下,方便在源码阅读时候进行调试编译,移植的过程总结如下心得. 1. openwrt编译与cl ...

  7. Python之数据结构:列表、元组、字典、set

    列表 列表里可以存储任意的数据类型.可修改的结构,用[ ]括起来表示或用函数list()构建. eg: y = [1,1.5,'hello',True] 列表还可以嵌套列表 eg: y = [1,1. ...

  8. LQB2017A02跳蚱蜢

    为什么第二题就这么难呜呜呜,这不是为难我吗!!! 可以明确的是,又是一个bfs 最少路径,找满足条件的那个层数 #include<iostream> #include<stdio.h ...

  9. 使用Gateway配置路由以及动态路由

    1. 新建module cloud-gateway-gateway9527 2. pom.xml <!--注意不需要web模块依赖,否则报错--> <?xml version=&qu ...

  10. __getattribute__小例子

    class student(object): def __init__(self,name=None,age=None): self.name = name self.age = age def __ ...