[英]Reading URLs from a HTML webpage and writing them to a file
我想創建一個簡單的程序,只讀取用戶指定網頁的源代碼中<a>
標記中包含的URL。 由於我是Java新手,因此不勝感激。 這是我到目前為止的內容,但這只會將整個網頁添加到文件中...
import javax.swing.*;
import java.net.*;
import java.io.*;
public class siteReader {
public static void main(String[] args) throws Exception {
URL address = new URL(JOptionPane.showInputDialog("Input a webpage address:"));
BufferedReader in = new BufferedReader(new InputStreamReader(address.openStream()));
}
}
Jsoup可以通過以下方式完成您想要的操作:
Document doc = Jsoup.connect(address).get();
Elements links = doc.select("a");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.