[英]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.