【Java】获取指定HTML 文档指定的body、页中超链接的标题和链接、指定博客文章的内容

说明:这个程序可以获取指定HTML 文档指定的body、页中超链接的标题和链接、指定博客文章的内容,不是很难理解。

需要用到的jar包链接:http://pan.baidu.com/s/1qWQ4TnI 密码:jdkk

用到的知识:javaIO

源代码:

import java.io.IOException;

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

public class JsoupTest {
    static String url="http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html";
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        
    	article();
    }

    /**
     * 获取指定HTML 文档指定的body
     * @throws IOException
     */
    private static void bolgBody() throws IOException {
    	
        // 直接从字符串中输入 HTML 文档
        String html = "<html><head><title> 开源中国社区 </title></head>"
                + "<body><p> 这里是 jsoup 项目的相关文章 </p></body></html>";
        Document doc = Jsoup.parse(html);
        System.out.println(doc.body());
        
        
        // 从 URL 直接加载 HTML 文档
        Document doc2 = Jsoup.connect(url).get();
        String title = doc2.body().toString();
        System.out.println(title);
    }

    /**
     * 获取网页中超链接的标题和链接
     */
    public static void article() {
        Document doc;
        try {
            doc = Jsoup.connect("http://news.baidu.com/ns?ct=0&rn=20&ie=utf-8&bs=%E6%88%90%E5%93%81%E6%B2%B9%E4%BB%B7&rsv_bp=1&sr=0&cl=2&f=8&prevct=no&tn=newstitle&word=%E6%88%90%E5%93%81%E6%B2%B9%E4%BB%B7").get();
            Elements ListDiv = doc.getElementsByAttributeValue("class","c-title");
            for (Element element : ListDiv) {
                Elements links = element.getElementsByTag("a");
                for (Element link : links) {
                    String linkHref = link.attr("href");
                    String linkText = link.text().trim();
                    System.out.println(linkHref);
                    System.out.println(linkText);
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    /**
     * 获取指定博客文章的内容
     */
    public static void blog() {
        Document doc;
        try {
            doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get();
            Elements ListDiv = doc.getElementsByAttributeValue("class","postBody");
            for (Element element :ListDiv) {
                System.out.println(element.html());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现将 HTML 的表格插入到 Word 指定面,并删除 HTML 没有表格的面,可以使用 Aspose.Words for Java 和 Jsoup 提供的以下代码: ``` // 读取 HTML 文件 File input = new File("input.html"); Document htmlDoc = Jsoup.parse(input, "UTF-8"); // 读取 Word 文件 Document doc = new Document("input.docx"); // 遍历 HTML 的所有表格 Elements tables = htmlDoc.select("table"); for (Element table : tables) { // 将表格转换为 Word 的表格 Table wordTable = HtmlTableImporter.importTable(table, true); // 将表格插入到 Word 指定面 int pageIndex = 1; // 指定要插入表格的面索引 if (doc.getPageCount() >= pageIndex) { Section section = doc.getSections().get(pageIndex - 1); Body body = section.getBody(); NodeCollection nodes = body.getChildNodes(); Node node = nodes.get(nodes.getCount() - 1); if (node instanceof Paragraph) { body.insertAfter(wordTable, node); } else { body.appendChild(wordTable); } } } // 删除 HTML 没有表格的面 Elements pages = htmlDoc.select("div.page"); for (Element page : pages) { if (page.select("table").isEmpty()) { page.remove(); } } // 保存 Word 文件 doc.save("output.docx"); ``` 这段代码会先读取 HTML 文件和 Word 文件,然后遍历 HTML 的所有表格,将每个表格转换为 Word 的表格,并将其插入到 Word 指定面。最后遍历 HTML 的所有面,如果某个没有表格,则删除该面。最后将处理后的文档保存为输出文件。 需要注意的是,这里使用了 Aspose.Words for Java 提供的 `HtmlTableImporter` 类将 HTML 表格转换为 Word 表格,如果你的项目没有使用 Aspose.Words,可以通过引入 Aspose.Words for Java 的 jar 包来使用该类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值