JSoup问题与祖先子操作符执行选择

时间:2012-10-17 23:56:51

标签: java screen-scraping jsoup

我正在尝试使用结果列类来删除div中的所有内容。

这是我用于查询的代码,它没有返回任何数据:

Elements el_name = doc.select(".div.results-column a.no-tracks.url"); 
Elements el_phone = doc.select(".div.results-column  span.business-phone.phone");
Elements el_address = doc.select(".div.results-column span.street-address");
Elements el_city = doc.select(".div.results-column span.locality");
Elements el_state = doc.select(".div.results-column span.region");
Elements el_postalcode = doc.select(".div.results-column span.postal-code");

此处列出了选择器:http://jsoup.org/cookbook/extracting-data/selector-syntax

示例:

<div class='results-column'>
   <div class='listing-content'>
   <span class='business-phone phone'>(111) 222-333</span><br>
   <span class='no-tracks url'>www.example.com</span><br>
   <span class='street-address'>29129 Sesame Street</span><span class='locality'>, Sesame City</span><br>
   [Rest of information from result1 would be here, I don't need to list every single thing on SO I hope]
   </div>
   <span class='business-phone phone'>(111) 222-333</span><br>
   <span class='no-tracks url'>www.example.com</span><br>
   <span class='street-address'>29129 Sesame Street</span><span class='locality'>, Sesame City</span><br>
   [Rest of information from result2 would be here, I don't need to list every single thing on SO I hope]
   </div>
</div>

我不能用祖先子操作符选择某个类的div吗?

1 个答案:

答案 0 :(得分:1)

应该是

  doc.select("div.results-column a.no-tracks.url");

不是

  doc.select(".div.results-column a.no-tracks.url");

令牌之前的点作为类选择器。 HTML标签不带点(其他选择器也一样)。