javaは腹を抱えて網の段をよじ登ります。


先に効果図を書きます
準備:
/**
 *   http  
 */
public static String Connect(String address) {
    HttpURLConnection conn = null;
    URL url = null;
    InputStream in = null;
    BufferedReader reader = null;
    StringBuffer stringBuffer = null;
    try {
        url = new URL(address);
        conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);
        conn.setDoInput(true);
        conn.connect();
        in = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        stringBuffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        conn.disconnect();
        try {
            in.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return stringBuffer.toString();
}
/**
 *             
 * @param allText
 */
private static void writeToFile(String allText) {
    System.out.println("    。。。");
    BufferedOutputStream bos = null;
    try {
        File targetFile = new File("/Users/shibo/tmp/pengfu.txt");
        File fileDir = targetFile.getParentFile();
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        bos = new BufferedOutputStream(new FileOutputStream(targetFile, true));
        bos.write(allText.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != bos) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println("    。。。");
}
jsoupを導入したjarパッケージ(解析dom):

    org.jsoup
    jsoup
    1.11.2
分析サイトを開始:
お腹を抱えてネットの段はまず私達の必要な内容(作者、タイトルと本文)を探し当てます。
その元素を調べてください。ここで見たのはタイトルラベルです。
その構造を知ると、私達が欲しい内容が手に入ります。
    public static void main(String[] args) {
        StringBuilder allText = new StringBuilder();
        for (int i = 1; i <= 50; i++) {
            System.out.println("     " + i + "   。。。");
            //     ,      
            String html = ConnectionUtil.Connect("https://www.pengfu.com/xiaohua_" + i + ".html");
            //       dom  ,    
            Document doc = Jsoup.parse(html);
            //            
            Elements titles = doc.select("h1.dp-b");
            for (Element titleEle : titles) {
                Element parent = titleEle.parent();
                //     
                String title = titleEle.getElementsByTag("a").text();
                //        
                String author = parent.select("p.user_name_list > a").text();
                //        
                String content = parent.select("div.content-img").text();
                //       
                allText.append(title)
                        .append("\r
:").append(author) .append("\r
").append(content) .append("\r
").append("\r
"); } allText.append("------------- ").append(i).append(" -------------").append("\r
"); System.out.println(" " + i + " 。。。"); } // Test.writeToFile(allText.toString()); }
参考文章:Python爬虫入門(一)——這い取りが悪い