其实很人都认为Python才能够做爬虫,其实C++与Java照样也是能够的,由于爬虫的原理很简单,无非就是剖析HTTP(s)央求,然后经过代码模拟浏览器去开源矿工建议央求,关于建议网络恳缓存视频怎样转入相册求结构的我选择的是Apa缓存视频怎样转入相册che的OKHttp,究竟自己手动拼接HTTP央求体仍是工作量比较大的一个工作。拿到网页后就需求github解析网页要害内容,此刻Jsoup就发挥作用了,经过节点选择器缓存视频兼并app下载 + 表达式能够很方便的拿到想要的数据,在我的开源项目中能够看到这个爬取进程的中心完毕,gitee.com/zouc线程池hanglin…

HttpClient

<线程安全dependency>
<groupId>org.apache.httpcomponents</多线程使用场景比方groupId>
<artifactId开源代码网站github>httpclient</artifactId>
<version>4.5.2</v缓存整理ersion>
</dependency>

Jsoup

咱们抓取到页面之后,还需求对页而进行解析。能够运用字符串处理东开源是什么意思西解析页面,也能够运用正则表达式,但是这些方法都会线程撕裂者带来很大的开发本钱,所以咱们需求运用一款专门解析HTML页面的缓存整理技术。

jsoup介绍

Jsoup是一款Java的HTML解析器,可直接解析某个URL地址、HTML文木内容。它供应了一套非常省力的gitiAPI,可经过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。

Jsoup的主要功能

1.从一个URL,文件或字符串中解析HTML;

2.运用DOM或CSS选择器来查找、取出数据;

3.可操作HTML元素、特征、文本;

Jsoup实战

<giti是什么牌子dependencies&线程同步gt;
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<git教程artifactId>线程池面试题httpclient</a缓存的视频怎样保存到本地rtifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12多线程面试题</artifactId>
<version>1.7.25</version>
</dependency>
<depend线程池原理ency>
<groupId>orgiti是什么牌子g.js缓存的视频怎样保存到本地oup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<ggithub中文官网网页roupId>commons-io</groupId>
<agit教程rti开源阅览app下载安装factId&g线程和进程的差异是什么t;commons-io</artifactId>
<v线程是什么意思ersion>2.6</缓存视频兼并version>
</多线程的完成方法dependency>
<dependency>
<groupId>org.apache.commgiti是什么牌子ons</groupId>
<artifactId>commons-lang3</线程artifactId>
&缓存视频怎样转入本地视频lt;version>3.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId多线程cpu有什么好处>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</depen缓存视频兼并dency缓存>
</dependencies>
@Test
public void testUrl() t缓存视频hrows Exception {
Docu线程的几种状况ment document = Jsoup.parse(new URL("http://zouchanglin.cn"), 5000);
String title = docume缓存整理nt.getElementsByTag("title").first().te多线程并发xt();
System.ou线程和进程的差异是什么t.print开源众包ln(title);
}
@Test
public void testString() throws Exception {
String html = FileUtils.readFileToString(缓存整理new Fil缓存视频e("C:Users15291Desktopindex.html"), "UTF-8");
Document documengit教程t = Jsoup.parse(html);
String tit线程le = document.getElementsByTag("title").first().text();
System.ou线程池原理t.println(title);
}
@Test
public void缓存的视频怎样保存到本地 testFile() throws Exceptio多线程的完成方法n {
Document document = Jsoup.parse(new File("C:多线程面试题及答案Users15291Desktopindex.html"开源), "UTF-8");
Str线程池的七个参数ing title = document.getElementsByTa开源矿工g("title").first缓存视频怎样转入相册().text();
System.out.println(titgiti是什么牌子le);
}

尽管运开源用Jsoup能够替代HttpClien多线程面试题及答案t直接建议央求解析数据,但是往往不会这样用,由于实践的开发进程中,需求运用到多线程,连接池,署理等等方法,而Jsou开源矿工p对这些的支撑并不是很好,所以咱们一般把Jsoup仅仅作为Html
解析东西运用。

Dom方法遍历文档

元素获取
1、依据id查询元素getElementByld
2、依据标签获取元素getElementsByTag
3、依据class获开源节流取元素getElementsByClass
4、依据特征获取元素getElementsB线程的几种状况yAttribute

元素中获取数据
1、从元素中获取id
2、从元素中获开源软件取className
3、从元素中获多线程面试题及答案取特征的值 attr
4、从缓存整理元素中获取悉数特征attributes
5、从元缓存视频变本钱地视频素中获取文本内容text

Selector选择器

tagname`:经过标签查找元素,比方: `span`
`#id`缓存的视频怎样保存到本地:经过ID查找元素,比方: `#city_bj`
`.class`:经过class称谓查找元素,比方: `.class_a
[attribute]`:运用特征查找元素,比方:`giti轮胎[abc]
[attr=value]`:运用特征多线程面试题值来查找元素,比方: `[class=s_name]

Selector选择器组合运用

el#id:元素+ID,比方:h3#3city_bj
el.class多线程使用场景比方:元素+class,比方:li.class_a
el[attr]:元素+特征名,比方:span[abc]
恣意组合:比方: span[abc].s_name
ancestor child:查找某个元素下子元素,比方: .city_con线程 li查找city_con下的悉数li
parent > child:查找某个父元素下的直接子元素,比方:.city_con > ul > li查找city_con榜首级(开源节流直接子元素)的ul,再找悉数ul下的榜首级li
parent &g线程池的七个参数t; *:查找某github个父元素下悉数直接子元素

package jsoup;
import org多线程面试题及答案.apach开源节流是什么意思是什么e.缓存视频变本钱地视频commons.io.F缓存视频兼并ileUtils;多线程面试题及答案
import org.jsoup.Jsoup;
import缓存整理 org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.n多线程的并发问题odes.Elemgithub永久回家地址ent;
import org.jsoup.sel开源节流是什么意思是什么ect.Elements;
import org.junit.Tesgithubt;
import java.io.Fi开源节流le线程同步;
import java.io.IOException;
import java.net.URL;
import java.uti多线程并发l.regex.M开源软件atcher;
import java.util.regex.Pattern;
public class JsoupFirstTest {
@Test
public void testUrl() throws Exception {
Document document = Jsoup.parse(new URL("http://zouchanglin.cn"), 5000);
Str缓存视频怎样转入相册ing title = document.getEle缓存的视频怎样保存到本地mentsByTag("title").first().text();
System.Gitout.println(title);
}
@Test
public void testString() throws Exception {
String html = FileUtils.readFileToString(new File("C:Users15291Desktopindex.html"), "UTF-8");
Document document = Jsoup.parse(html);
String t线程池原理itl开源众包e = document.getElementsByTag("title").first().text(开源代码网站github);
System.out.println(title);
}
@Test
public void testFile() throws Exception {
Document document = Jsoup.parse(缓存视频在手机哪里找new File("C:Users15291Desktopindex.html"), "UTF-8");
String title = document.getElementsByTag缓存视频怎样转入相册("title").first().text();
System.out.println(title);
}
@Test
public void testDom() throws Exception {
/**
* 1、依据id查询元素getElementBy开源节流ld
* 2、依据标签获取元素getElementsByTag
* 3、依据class获取元素getElgithub永久回家地址ementsByClass
* 4、依据特征获取元素getElements线程池原理ByAttribute
*/
Document document = Jsoup.parse(new File("C:Users15291Desktopindex.html"), "UTF-8")github中文官网网页;
Element element = document.getElementById("threeSpan");
System.out.print多线程下载ln("threeSpan内容是:" + element);
System.out.println(element.多线程编程getElementsByTag("a").first().attr("href"));
Elements spans = document.getElementsByTag("span");github
for(Egithublement el: spans) System.out.println(el);
System.out.println(document.getEgiteele多线程使用场景比方me线程同步ntsByAttributeValue(多线程使用场景比方"type", "buttgiteeon").first());
System线程池面试题.out.println(document.getElementsByAttributeValue("type"Git, "button").first().attr("value"));
/*缓存视频变本钱地视频*
* 1、从元素中获取id
* 2、从元素中获取className
* 3、从元素中获取特征的值 attr
* 4、从元素中获取悉数特征attributes
* 5、从元素中获取文本内容text
*/
Element button = document.getElementsByAttribut线程池面试题eValue("t线程安全ype", "button").first();
Attributes attributes = button.a缓存整理ttributes();
System.out.println(a线程池的七个参数ttribu线程tes);
}
@Test
public void testSelector() throws Exceptio开源软件n {
/**
* `tagname`:经过标签查找元素,比方: `span`
* `#id`:经过ID查找元素,比方: `#city_bj`
* `.class`:经过class称谓查找元素,比方: `.class_a`
* `[attribute]`:运用特征查找元素,比方:`[abc]`
* `[attr=value]`:运用特征值来查找元素,比方: `[class=s_缓存视频在手机哪里找name]`
*/
Document document = Jsoup.多线程使用场景比方parse(newgiti轮胎 File缓存视频兼并(github"C:Users15291Desktopindex.html"), "缓存视频变本钱地视频UTF-8");
Elements span = document.select("span");
System.out.println(span);
System.out.println("===============================");
System.out.pr线程池面试题intln(document.select("#threeSpan").first缓存视频怎样转入相册());
System.out.println(docu多线程使用场景比方ment.select("#threeSpan").fgitiirst().text());
}
@Test
public void testSelectorTwo() throws Exception {
/**
*缓存的视频怎样保存到本地 `el#id`:元素+Igit教程D,比方:`h3#3city_bj`
* `el.class`:元素+class,比方:`li.class_a`
* `el[attr]`:元素+特征名,比方:`span[abc]`
* 恣意组合:比方: `span[abc].s_name`
* `开源ancestorgit教程 child`:查找某个元素下子元素,比方: `.city_con li`查找""city_con"下的悉数li缓存视频
* `parent > child`:查找某个父元缓存的视频怎样保存到本地素下的直接子元素,比方缓存的视频怎样保存到本地:`.city_con > ul > li`查找city开源阅览app下载安装_con榜首级(直接子元素)的ul,再找悉数ul下的榜首级li
* `parent > *`:查找某个github中文官网网页父元素下悉数直接子元素
*/
D缓存的视频怎样保存到本地ocument document = Jsoup.par缓存视频se(new File("C:Users15291Desktopindex.html"), "UTF-8");
System.out开源节流是什么意思是什么.println(document.select("span#oneSpan").fi开源众包rst());
Sys缓存整理tem.o缓存视频怎样转入相册ut.prin缓存视频tln(document.select("span[style]").f开源节流是什么意思是什么irst());
System.out.println(document.select(".my_div div"));
Syst缓存文件在哪里em.out.println("开源节流是什么意思是什么============================缓存视频==========");
Syst开源节流emgithub中文官网网页.out.println(docu缓存视频怎样转入相册ment.selec线程安全t(".my_div > div"开源众包));
System.out.开源众包println("======================================");缓存整理
System.out.println(document.se多线程使用场景比方lect(".my_多线程是什么意思div *"));
}
@Test
public void course() throws IOException {
Document document = Jsoup.parse(new File("C:Users15291githubDesktopnew 10.html"), "UTF线程-8");
Element tbody = document缓存视频兼并.select("table.blacktab > tbody").first();
Elements tds = tbody.select("tr td");
for(Element td: tds){
String tdContent =多线程面试题及答案 td.text();
if(tdContent.contains("{")){
System.out.prin多线程tln(tdContent);
hand缓存el(缓存视频怎样转入本地视频tdContent);
}
}
}
private void多线程使用场景比方 handel(String tdCo线程池ntent) {
Str多线程的完成方法ing[] split = tdContent.split(" ");
System.out.print(spl多线程是什么意思it[0] + "t");
System.out.print(split[1].su开源阅览app下载安装bstring(0, 2) + "t");
System.out.print(split[1].substring(split[1].indexOf("第")+1, split[1].indexOf("节")) + "t");
Systgiti轮胎em.out.print(split[1].substring(split[1].indexOf("{")+2, split[1].indexOf("}") - 1) + "t")缓存视频兼并app下载;
System.out.print(split[2] + "t");
System.缓存视频怎样转入相册out.print(split[3] + "t");
Sygiti是什么牌子stem.out.println("");
}
}

正方教务爬虫

1、引进多线程依托

<dependencies>
<dependency>
<groupId>com.gitee.zouchanglin</gr线程安全oupId>
<artifactId>giti是什么牌子sp线程池原理ider_xpu</artifactId>
<version缓存视频兼并>1.2</version>
</dependency>
</dependencies>
<repositorigithubes>
<repository>
<id>jitp多线程ack.io</id>
<url>https开源阅览://jitpack.io</url>
</reposit开源中国or缓存视频怎样转入本地视频y>
</repositories>

2、运用示例

import cn.zouchanglin.spider_开源阅览xpu.SpiderResult线程安全;线程撕裂者
imporgit教程t cn.zouchanglin.spider_xpu.cache.Result线程的几种状况Cache;
import cn.zouchanglin.spider_xpu.core.SpiderCore;
import javax.security.auth.login.LoginException;
import java.awt.开源是什么意思*;
import开源 java.net.URI;
import java.util.Scannegiti是什么牌子r;
publi缓存视频变本钱地视频c class Main {
public static void main(String[] args) throws Exce多线程编程ption {
// Key就是一个标识用户仅有的键(如能够传OpenI多线程的完成方法d、UnionId、学号、身份证号等)
// TODO 填充Key、us缓存视频兼并erId、password等字段
String key = "";
String use开源众包rId = "";
String passsword = "";
// 1、获取验证码URL
String url = SpiderCore.g缓存视频在手机哪里找etCheckCodeUrl(key);
// 翻开浏览器并输入验证码
Desktop desktop = Desk线程和进程的差异是什么top.ge开源节流是什么意思是什么tDesktop();
if (Desktop.isDesktopSup缓存的视频怎样保存到本地ported() && desktop.isSupported(Desktop.Action.BROWSE)) {
URI uri = new URI缓存(url);
desktop.browse(uri多线程并发);
}
Scanner s线程池面试题canner = new Scanner(Syste缓存视频怎样转入本地视频m.in);
String code = scanner.nextLine();
// 记github中文官网网页载用时
long millis = System.currentTimeMillis();
SpiderResult spiderResult = null;
try {
// 2、获取同步调用作用只回来用户信息 + 其时学年的课表
spiderResult = SpiderCore.go缓存视频变本钱地视频(userId, password, code, key);
}catch (L缓存视频兼并oginException e){
// 登录失败
System.out.println开源阅览(e.toString());
}
// 同步调用作用
System.out.println("同步调用只回来用户信息+其时学年的课表:" + spiderResult);
System.out.println("实行耗时:" + (System.currentTim线程是什么意思eMillis() - millis));
// 堵塞等候缓存池中存在作用政策
while(!ResultCac线程撕裂者he.SPIDER_RESULT_CACHE.c缓存视频怎样转入相册ontainsKey(key));
// 取缓存的视频怎样保存到本地出缓存池中的作用
SpiderResult result = ResultCache.SPIDER_RESULT_CACHE.get(key);
System.out.println(result);
//TODO 耐久化
System.out.prin开源矿工tln("完毕耐久化....");
// 从缓存池中移除作用政策
ResultCache.SPIDER_RESULT_CACHE.remove(key);
}
}

经过对学开源节流生信息和学生悉数课表的爬取,作用还不错!线程池

Jsoup实战(正方教务体系爬取)