【问题标题】:JSoup - select more than one itemJSoup - 选择多个项目
【发布时间】:2013-03-16 12:36:24
【问题描述】:

我正在尝试从这个文件中提取信息:

<tr id="ctl00_Body_mc_cErgebnisListe1_ctl02_InseratInfoTR" class="topangebot"> 
 <td class="BildTD" rowspan="2"> <a href="/anzeiger/immoweb/Detail.aspx?InseratID=6629161&amp;FromTopAngebot=true"><img border="0" src="http://images.derstandard.at/t/22/upload/imagesanzeiger/immoupload/2012/05/73/733de246-b4eb-425a-8705-2e8b50baff12.jpg" alt="" /></a> </td> 
 <td class="TitleTD" rowspan="2"> <span class="neu">TOP!</span> <strong><a href="/anzeiger/immoweb/Detail.aspx?InseratID=6629161&amp;FromTopAngebot=true">Ihr Geld als sichere Anlage - Eigentum vom Feinsten - Jacquingasse 29</a></strong><br /><a href="/anzeiger/immoweb/Detail.aspx?InseratID=6629161&amp;FromTopAngebot=true">Wien 3.,Landstra&szlig;e, Wohnung</a><br /><span style="color: gray">Erstbezug, Parkettboden, Lift, Provisionsfrei, Kabel/Sat-TV</span> </td> 
 <td class="GroessenTD" rowspan="2"> </td> 
 <td class="PreisTD" style="border:none;"> </td> 
</tr>
<tr id="ctl00_Body_mc_cErgebnisListe1_ctl02_InseratInfoTR" class="topangebot"> 
 <td class="BildTD" rowspan="2"> <a href="/anzeiger/immoweb/Detail.aspx?InseratID=6629161&amp;FromTopAngebot=true"><img border="0" src="http://images.derstandard.at/t/22/upload/imagesanzeiger/immoupload/2012/05/73/733de246-b4eb-425a-8705-2e8b50baff12.jpg" alt="" /></a> </td> 
 <td class="TitleTD" rowspan="2"> <span class="neu">TOP!</span> <strong><a href="/anzeiger/immoweb/Detail.aspx?InseratID=6629161&amp;FromTopAngebot=true">Ihr Geld als sichere Anlage - Eigentum vom Feinsten - Jacquingasse 29</a></strong><br /><a href="/anzeiger/immoweb/Detail.aspx?InseratID=6629161&amp;FromTopAngebot=true">Wien 3.,Landstra&szlig;e, Wohnung</a><br /><span style="color: gray">Erstbezug, Parkettboden, Lift, Provisionsfrei, Kabel/Sat-TV</span> </td> 
 <td class="GroessenTD" rowspan="2">12312 </td> 
 <td class="PreisTD" style="border:none;">3123 </td> 
</tr>

我想用我的 css 查询同时从同一个 tr.topangebot 中选择标题和价格。我试过这个查询:

Elements topangebotPars = doc.select("tr.topangebot > (td.TitleTD && td.GroesseTD)");

我明白了:

无法解析查询

如何同时选择多个元素?

【问题讨论】:

    标签: java web web-scraping jsoup


    【解决方案1】:

    请记住,select 使用的选择器是 CSS 选择器。因此,就像您在 CSS 文件中编写 comma-separated list of selectors 一样,例如:

    tr.topangebot > td.TitleTD, tr.topangebot > td.GroesseTD {
        color: blue:
    }
    

    ...你用select做同样的事情:

    Elements topangebotPars = doc.select("tr.topangebot > td.TitleTD, tr.topangebot > td.GroesseTD");
    

    JSoup 文档中有一个quick reference 用于选择器。他们将其列在“组合器”下(尽管 CSS 规范本身并不认为它们是“组合器”,只是一个组)。

    【讨论】:

    • 谢谢你的回答!!!目前我也得到了 html 属性。你知道如何摆脱它们吗?以及如何插入分隔符以更轻松地切割字符串?
    • @maximus:我不太了解 JSoup,无法了解属性方面。至于另一个,您可以使用字符串连接将它们写在单独的行上。
    猜你喜欢
    • 2011-09-08
    • 2022-12-10
    • 2020-12-19
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多