我想从网页中解析数据,可以提供以下内容:
<div class="InseratDaten">
<div class="Art">Rent</div>
<div class="Ort">TestCity 3., Roads Street</div>
<div class="Preis"><span class='Label'>Miete:</span> 950 EUR</div>
<div class="Groesse"><span class='Label'>Fläche:</span> 72 m²</div>
<div class="Zimmer"><span class='Label'>Zimmer:</span> 3</div>
</div>
然而,有时这些结构看起来完全不同,如:
<div class="InseratDaten">
<div class="Art">Rent</div>
<div class="Ort">Test 3., Road Street</div>
<div class="Preis"><span class='Label'>Miete:</span> 919 EUR</div>
<div class="Groesse"><span class='Label'>Fläche:</span> 84 m²</div>
<div class="Zimmer"><span class='Label'>Zimmer:</span> 3</div>
<div class="EigTitel">weitere Eigenschaften:</div>
<div class='EigListe'>Shower, Balcony, Dog</div>
</div>
或
<div class="InseratDaten">
<div class="Art">Sale</div>
<div class="Ort">Test 4., Road Street</div>
<div class="Preis"><span class='Label'>Miete:</span> 919 EUR</div>
<div class="Groesse"><span class='Label'>Fläche:</span> 84 m²</div>
</div>
如何在Jsoup中解析它,以便当缺少的字段获得空值并且我的数据保持一致时。
目前我正在解析这样的数据:
Document doc = Jsoup.parseBodyFragment(html);
Elements title = doc.select("div[class=Title]");
Elements art = doc.select("div[class=Art]");
Elements location = doc.select("div[class=Ort]");
Elements price = doc.select("div[class=Preis]");
Elements size = doc.select("div[class=Groesse]");
Elements numberOfRooms = doc.select("div[class=Zimmer]");
Elements furtherProperties = doc.select("div[class=EigListe]");
titleList = new ArrayList(); artList = new ArrayList(); locationList = new ArrayList(); priceList = new ArrayList(); sizeList = new ArrayList(); numberOfRoomsList = new ArrayList(); FurtherPropertiesList = new ArrayList();
//price
for (Element element : price) {
priceList.add(element.text().toString());
}
//size
for (Element element : size) {
sizeList.add(element.text().toString());
}
//numberOfRooms
for (Element element : numberOfRooms) {
numberOfRoomsList.add(element.text().toString());
}
//furtherProperties
for (Element element : furtherProperties) {
furtherPropertiesList.add(element.text().toString());
}
//location
for (Element element : location) {
locationList.add(element.text().toString());
}
//art
for (Element element : art) {
artList.add(element.text().toString());
}
//title
for (Element element : title) {
titleList.add(element.text().toString());
}
log.info(ListstoString());
//add everything to the main domain List
for (int i = 0; i < locationList.size(); i++) {
Property prop = new Property();
//price
prop.setPrice(priceList.get(i));
//size
prop.setSize(sizeList.get(i));
//number of rooms
prop.setNumberOfRooms(numberOfRoomsList.get(i));
//furtherProperties
prop.setFurtherProperties(furtherPropertiesList.get(i));
//location
prop.setLocation(locationList.get(i));
//art
prop.setTransactionType(artList.get(i));
//title
prop.setTitle(titleList.get(i));
//set date
prop.setCrawlingDate(new Date());
list.add(prop);
}
任何建议如何实现?
答案 0 :(得分:2)
选择可用元素后,请先使用for
循环代替foreach
,然后在将元素添加到!null
之前“检查”每个元素是否为arrayList
。
Document doc = Jsoup.parseBodyFragment(html);
Elements title = doc.select("div[class=Title]");
Elements art = doc.select("div[class=Art]");
Elements location = doc.select("div[class=Ort]");
Elements price = doc.select("div[class=Preis]");
Elements size = doc.select("div[class=Groesse]");
Elements numberOfRooms = doc.select("div[class=Zimmer]");
Elements furtherProperties = doc.select("div[class=EigListe]");
//for price
Int i;
for (i=0; i < element.length; i++) {
if(element[i] != null){
priceList.add(element.text().toString());
}else{
priceList.add("null/undef/whatever");
}
}
//同样适用于其他类
//进一步处理,检查列表中的每个元素是否为'null / undef / whatever'