我正在使用JSoup构建网络抓取工具。我试图从下面的HTML代码中从img类中提取标题。

<div id="insideScroll" class="grid slider desktop-view">
    <ul class="ng-scope" ng-if="2 === selectedCategoryId">
      <li class="" data-list-item="">
          <span>
              <a class="grid-col--subnav ng-isolate-scope" data-internal-referrer-link="hub nav" data-link-name="hub nav daughter" data-click-id="hub nav 2" href="/recipes/111/appetizers-and-snacks/beans-and-peas/?internalSource=hub nav&referringId=76&referringContentType=recipe hub&linkName=hub nav daughter&clickId=hub nav 2" target="_self">
                  <img class="" alt="Bean and Pea Appetizers" title="Bean and Pea Appetizers" src="http://images.media-allrecipes.com/userphotos/140x140/00/60/91/609167.jpg">
                  <span class="category-title">Bean and Pea Appetizers</span>
              </a>
         </span>
     </li>
</div>


这是我所拥有的功能,但似乎不起作用。我在运行它时收到一个Null Pointer Exception,我假设它来自堆栈跟踪是由于图像类中缺少名称。我也可以从span类中提取标题,但是也很难从中获取文本。感谢您的帮助。

@Override
public ArrayList<String> parseDocForTitles(Document doc) {
    ArrayList<String> titles = new ArrayList<>();
    String title;

    Element insideScroll = doc.getElementById("insideScroll");
    Elements img = insideScroll.select("img.\"\"");

    for(Element ttle : img){
        title = ttle.attr("title");
        out.println(title); //just for testing
        titles.add(title);
    }

    return titles;
}


以下是我收到的堆栈跟踪:

[-]ERROR: See Stack Trace
java.lang.NullPointerException
    at Scraper.Appetizers.parseDocForTitles(Appetizers.java:35)
    at Scraper.Driver.main(Driver.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

最佳答案

这对我有用:

Document document;
try { //Get Document object after parsing the html from given url.
    document = Jsoup.connect(yourURL).get();
    //Get images from document object.
    Elements images = document.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
    //Iterate images and print image attributes.
    for (Element image : images) {
        System.out.println("Image Source: " + image.attr("title"));
    }
} catch (IOException e) {
    e.printStackTrace();
}

09-25 20:56