且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Jsoup图像标记提取

更新时间:2023-12-03 20:39:58

以下是获取图像源属性的示例:

Here's an example to get the image source attribute:

public static void main(String... args) {
    Document doc = Jsoup.parse("<div class=\"picture\"><img src=\"http://asdasd/aacb.jpgs\" title=\"picture\" alt=\"picture\" /></div>");
    Element img = doc.select("div.picture img").first();
    String imgSrc = img.attr("src");
    System.out.println("Img source: " + imgSrc);
}

div.picture img selector在div下找到image元素。

The div.picture img selector finds the image element under the div.

元素的主提取方法是:


  • attr(name),它获取元素属性的值,

  • text(),它获取元素的文本内容(例如,在< p> Hello< / p> 中,text()是你好),

  • html(),它获取一个元素的内部HTML(< div> < img>< / div> html()= < img> )和

  • outerHtml(),它获取完整的HTML元素(< div>< img>< / div> html()= < div>< img>< / div>

  • attr(name), which gets the value of an element's attribute,
  • text(), which gets the text content of an element (e.g. in <p>Hello</p>, text() is "Hello"),
  • html(), which gets an element's inner HTML (<div><img></div> html() = <img>), and
  • outerHtml(), which gets an elements full HTML (<div><img></div> html() = <div><img></div>)

您不需要像当前示例那样重新分析HTML,要么使用更具体的选择器首先选择正确的元素,要么点击 element.select(string) 获胜的方法。

You don't need to reparse the HTML like in your current example, either select the correct element in the first place using a more specific selector, or hit the element.select(string) method to winnow down.