gpt4 book ai didi

java - 如何迭代 jsoup 标签,但忽略前两个发现?

转载 作者:太空宇宙 更新时间:2023-11-04 06:37:51 28 4
gpt4 key购买 nike

这就是我想要从中获取日期的内容:

<div class='col5'>
<strong><a href="/stellenangebote/107278-supporter-sap-crm?page=1&amp;query%5Bcity%5D=&amp;query%5Bradius%5D=100&amp;query%5Btext%5D=SOA+SAP" title="Supporter SAP CRM (m/w)">Supporter SAP CRM (m/w)</a></strong>
<br>
<a class="job-offer-teaser-company" href="/unternehmen/vesterling-consulting-gmbh" title="Vesterling Consulting GmbH">Vesterling Consulting GmbH</a>
</div>
<div class='col2'>
bundesweit
</div>
<div class='col2'>
12.02.2014
</div>

问题是,顶部还有两个 col5 容器,我想在运行此代码时跳过:

Document document = Jsoup.parse(new File("C:/Talend/workspace/WEBCRAWLER/output/absolventa_keywords_"+row[i].trim()+".txt"), "utf-8");
Elements elements = document.select(".col5");
for (Element element : elements)
{
// Parse Data into Elements
Elements jobTitleElement = element.select("a");
Elements companyNameElement = element.select("a.job-offer-teaser-company");
// problem: Location and Date Both in class "col2".
Elements col2Elements = document.select("div.col2"); //This will return a collection of Element objects

// Get them in element form and split it with get(0,1)
String location = col2Elements.get(4).text(); //Get the first
String date = col2Elements.get(5).text(); //Get the second

// format date
Date timeAdded = new SimpleDateFormat("dd.MM.yyyy").parse(date);

// Strip Data from unnecessary tags
String companyName = companyNameElement.text();
String jobTitle = jobTitleElement.text();
System.out.println(companyName+ jobTitle + location + timeAdded);

如何在迭代其他元素时跳过这些元素?谢谢!

最佳答案

如果你想跳过 2 个第一个元素,那么如何使用

for (int i = 2; i<elements.size(); i++) 
{
Element element = elements.get(i);

而不是

for (Element element : elements) 
{

而且你不应该调用

Elements col2Elements = document.select("div.col2");//This will return a collection of Element objects

在一个循环中,因为你总是得到相同的结果。与此相同

String location = col2Elements.get(0).text(); //Get the first
String date = col2Elements.get(1).text(); //Get the second

只需在循环之前调用一次即可。

关于java - 如何迭代 jsoup 标签,但忽略前两个发现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25118742/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com