java how to traverse html page url connection

Functional Description

In this paper, sina get home, for example, describes how to use java url links to obtain the address of the web page.

Sample Code


import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

public class App {
	public static void main(String[] args) {
		parse();
	}
public void parse() {

		Document doc;
		try {
			int cnt = 0;
			String url = "https://www.sina.com.cn/";
			doc = Jsoup.connect(url).get();
			Elements rows = doc.select("div ul li");
			if (rows.size() > 0) {
				for (Element row : rows) {
					String link = row.select("a").attr("href");
					String title = row.select("a").text();
					System.out.println((++cnt) + ":\t" + title + "\t" + link);// 获取文件链接地址
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output:

Here Insert Picture Description

Published 230 original articles · won praise 29 · Views 230,000 +

Guess you like

Origin blog.csdn.net/huryer/article/details/104078018