来自img类的JSoup Extract标题

时间:2017-01-10 18:31:34

标签: java web-scraping jsoup

我正在使用JSoup构建一个Web scraper。我试图从下面的HTML代码中提取出img类的标题。

multipart/form-data

这是我所拥有的功能,但它似乎并没有起作用。我在运行它时收到空指针异常,从堆栈跟踪中我假设是由于图像类中缺少名称。我也可以从span类中提取标题,但是也无法从中获取文本。谢谢你的帮助。

<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>

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

@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;
}

2 个答案:

答案 0 :(得分:2)

这对我有用:

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(); 
}   

答案 1 :(得分:0)

您只需要正确选择img元素。

改变这个:

Elements img = insideScroll.select("img.\"\"");

对此:

Elements img = insideScroll.select("img");

它应该有用。