jsoup absURL()方法详解
时间: 2023-12-07 18:02:59 浏览: 29
好的,关于jsoup的absURL()方法的详解,absURL()方法是用来解析相对路径URL并返回绝对路径URL的方法。它可以将网页中的相对路径URL(如“/article/123.html”)转换成绝对路径URL(如“https://www.example.com/article/123.html”)。这样就可以方便地获取网页中的所有链接,并可以在不同页面之间进行导航。
相关问题
jsoup的select方法
Jsoup是一款Java的HTML解析器,可以用来解析HTML文档、查找和提取数据。其中,select方法是Jsoup提供的一种查找HTML元素的方式,可以根据元素的选择器进行查找。
以下是select方法的基本用法:
```java
Document doc = Jsoup.connect("https://www.example.com").get();
Elements elements = doc.select("p"); // 查找页面上的所有<p>元素
```
这样就可以从网页中提取出所有的<p>元素。select方法的参数可以是任意的CSS选择器,例如:
```java
Elements links = doc.select("a[href]"); // 查找所有具有href属性的<a>元素
Element firstLink = doc.select("a[href]").first(); // 查找第一个具有href属性的<a>元素
Elements pngs = doc.select("img[src$=.png]"); // 查找所有src属性以.png结尾的<img>元素
```
除了基本的CSS选择器,select方法还支持一些扩展的选择器,例如:
- :contains(text):查找包含指定文本的元素
- :not(selector):查找不匹配指定选择器的元素
- [attribute^=value]:查找指定属性值以value开头的元素
- [attribute$=value]:查找指定属性值以value结尾的元素
- [attribute*=value]:查找指定属性值包含value的元素
更多选择器的使用方法可以参考官方文档:https://jsoup.org/cookbook/extracting-data/selector-syntax
jsoup post
以下是使用Jsoup进行POST请求的示例代码:
```java
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
public class JsoupPostExample {
public static void main(String[] args) {
try {
Document document = Jsoup.connect("http://www.example.com/login")
.data("username", "myUsername")
.data("password", "myPassword")
.userAgent("Mozilla/5.0")
.timeout(3000)
.post();
Elements elements = document.select("div.myClass");
System.out.println(elements.text());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个例子中,我们使用Jsoup的connect()方法来连接到一个URL,并使用data()方法来添加POST请求的参数。然后,我们使用userAgent()方法来设置User-Agent头,timeout()方法来设置超时时间,最后使用post()方法来发送POST请求。我们可以使用select()方法来选择我们需要的元素,并使用text()方法来获取元素的文本内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)