[英]Jsoup Java get a specific td
我有以下代码
import java.io.IOException;
import java.util.*;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
public class da {
/**
* @param args
*/
public static void main(String[] args) {
try {
Document doc=Jsoup.connect("http://www.vremea.net/").get();
Elements e=doc.select(".homeContent ul li a ");
PrintStream ps=new PrintStream(new FileOutputStream("io"));
String rezultat="";
for(int i=0;i<e.size();i++)
if(e.get(i).attr("href").contains("Arad"))
rezultat=e.get(i).attr("href");
System.out.println(rezultat);
Document doc1=Jsoup.connect(rezultat).get();
Elements row=doc1.select(".tableforecast tr");
Elements nume=doc1.select("h1");
ArrayList<String> date=new ArrayList<String>();
ArrayList<String> numedate=new ArrayList<String>();
for(int q=1;q<nume.size();q++)
if(nume.get(q).text().contains("Vremea in"))
numedate.add(nume.get(q).text());
for(int i=0;i<row.size();i++)
{
Elements col=row.get(i).select("td");
String sir="";
int vr=0;
for(int j=0;j<col.size();j++)
if(col.get(j).className().equals("cell large"))
{sir=sir+" "+col.get(j).text();
vr=1;}
if(vr==1)
date.add(sir);
}
for(int i=0;i<numedate.size();i++){
for(int j=0;j<date.size();j=j+2)
ps.println(numedate.get(i)+"\n"+date.get(j)+"\n"+date.get(j+1));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这段代码进入一个表,对于每一行,它得到一个包含一些字符串的列。 我想知道是否可以不使用contains直接获取那些列并获取所有列,然后从所有这些列中获取所需的信息,我想知道如果可能的话将选择什么样?
numedate-是日期的名称,date是温度和小时。
您可以这样尝试:
直接转到您要提取数据的页面(在您的情况下为“ Arad”)
“ http://www.vremea.net/Vremea-in-Arad-judetul-Arad/prognoza-meteo-pe-7-zile ”请参阅其他页面。 他们似乎具有某种结构,例如:/ some-place-text name-some text / some text
您可以直接在类单元格和大类中选择td元素,如下所示
public static void main (String [] args) throws IOException{
Document doc = Jsoup.connect("http://www.vremea.net/Vremea-in-Arad-judetul-Arad/prognoza-meteo-pe-7-zile").get();
Elements tds = doc.select("table.tableforecast tbody tr td.cell.large");
for (Element e : tds){
System.out.println(e.text());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.