使用requests爬取网页:Python网页解析库:用requests-html爬取网页
Python网页解析库:用requests-html爬取网页 1. 开始 Python 中可以进行网页解析的库有很多,常见的有 BeautifulSoup 和 lxml 等。在网上玩爬虫的文章通常都是
顺晟科技
2021-09-23 10:51:46
82
Java爬虫解析HTML文档的工具有:htmlparser, Jsoup。本文将会详细介绍Jsoup的使用方法,10分钟搞定Java爬虫HTML解析。
Jsoup可以直接解析某个URL地址、HTML文本内容,它提供非常丰富的处理Dom树的API。如果你使用过JQuery,那你一定会非常熟悉。
Jsoup最强大的莫过于它的CSS选择器支持了。比如:document.select("div.content > div#image > ul > li:eq(2)。
添加下面的依赖声明即可,最新版本是(1.12.1)
<dependency>
<!-- jsoup HTML parser library @ https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
// jsoup HTML parser library @ https://jsoup.org/
compile \'org.jsoup:jsoup:1.11.3\'
当然也可以直接把jar包下载下来,下载地址:https://jsoup.org/download
# git获取代码
git clone https://github.com/jhy/jsoup.git
cd jsoup
mvn install
# 下载代码
curl -Lo jsoup.zip https://github.com/jhy/jsoup/archive/master.zip
unzip jsoup.zip
cd jsoup-master
mvn install
Jsoup支持四种方式解析Document,即可以输入四种内容得到一个Document:
body片段字符串中必须包含head和body元素。
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
String html = "<div><p>Lorem ipsum.</p>";
Document doc = Jsoup.parseBodyFragment(html);
Element body = doc.body();
Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();
还可以携带cookie等参数:
Document doc = Jsoup.connect("http://example.com")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Jsoup封装并实现了DOM里面常用的元素遍历方法:
getElementById(String id)getElementsByTag(String tag)getElementsByClass(String className)getElementsByAttribute(String key)siblingElements(), firstElementSibling(), lastElementSibling(); nextElementSibling(), previousElementSibling()parent(), children(), child(int index)这些方法会返回Element或者Elements节点对象,这些对象可以使用下面的方法获取一些属性:
attr(String key): 获取某个属性值attributes(): 获取节点的所有属性id(): 获取节点的idclassName(): 获取当前节点的class名称classNames(): 获取当前节点的所有class名称text(): 获取当前节点的textNode内容html(): 获取当前节点的 inner HTMLouterHtml(): 获取当前节点的 outer HTMLdata(): 获取当前节点的内容,用于script或者style标签等tag(): 获取标签tagName(): 获取当前节点的标签名称有了这些API,就像JQuery一样很便利的操作DOM。
你可能会说htmlparse支持xpath,可以很方便的定位某个元素,而不用一层一层地遍历DOM树。调用方法如下:
document.select(String selector): 选择匹配选择器的元素,返回是Elements对象document.selectFirst(String selector): 选择匹配选择器的个元素,返回是一个Element对象element.select(String selector): 也可以直接在Element对象上执行选择方法Jsoup能够完美的支持CSS的选择器语法,可以说对应有前端经验的开发者来说简直是福音,不用特意去学习XPath的语法。
比如一个XPath: //*[@id="docs"]/div[1]/h4/a, 可以转成等效的CSS选择器语法: document.select("#docs > div:eq(1) > h4 > a").attr("href");。
看下面的示例:
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "/uploads/image/20210923/error.html]"); // img with src ending .png
Element masthead = doc.select("div.masthead").first(); // div with class=masthead
Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
下面列出一些常见的选择器:
div): tag#logo): #id.head): .class[href]): [attribute][attr=value][^attr][attr^=value], [attr$=value], [attr*=value], [attr~=regex]另外还支持下面的组合选择器:
element#id: (div#logo: 选取id为logo的div元素)element.class: (div.content: 选择class包括content的div元素)element[attr]: (a[href]: 选择包含href的a元素)ancestor child: (div p: 选择div元素的所有p后代元素)parent > child: (p > span: 选择p元素的直接子元素中的span元素)siblingA + siblingB: (div.head + div: 选取div.head的下一个兄弟div元素)siblingA ~ siblingX: (h1 ~ p: 选取h1后面的所有p兄弟元素)el, el, el: (div.content, div.footer: 同时选取div.content和div.footer)当然,还支持伪元素选择器:
:lt(n): (div#logo > li:lt(2): 选择id为logo的div元素的前3个li子元素):gt(n):eq(n):has(selector):not(selector):contains(text)详细可参考官方选择器语法文档: https://jsoup.org/cookbook/extracting-data/selector-syntax
当然Jsoup还支持修改DOM树结构,真的很像JQuery。
// 设置属性
doc.select("div.comments a").attr("rel", "nofollow");
// 设置class
doc.select("div.masthead").attr("title", "jsoup").addClass("round-box");
下面的API可以直接操作DOM树结构:
text(String value): 设置内容html(String value): 直接替换HTML结构append(String html): 元素后面添加节点prepend(String html): 元素前面添加节点appendText(String text), prependText(String text)appendElement(String tagName), prependElement(String tagName)Jsoup官网地址: https://jsoup.org/Jsoup官网指导文档: https://jsoup.org/cookbook/Jsoupjar包下载地址: https://jsoup.org/downloadJsoupCSS选择器参考: https://jsoup.org/cookbook/extracting-data/selector-syntax30
2022-09
25
2022-09
15
2022-09
15
2022-09
15
2022-09
15
2022-09