java - Jsoup Java 获取特定的td

标签 java html web-scraping jsoup html-table

我有以下代码

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- 是日期的名称,日期是温度和小时。

最佳答案

你可以这样尝试:

直接转到您要提取数据的页面(在您的情况下为“Arad”)

http://www.vremea.net/Vremea-in-Arad-judetul-Arad/prognoza-meteo-pe-7-zile”查看其他页面。他们似乎有某种结构,例如: /一些文本就地名称-一些文本/一些文本

您可以直接选择类单元格和类大中的 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());
    }
}

关于java - Jsoup Java 获取特定的td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38789736/

相关文章:

java - UIMA RUTA - 如何组合注释?

java - 屏幕关闭时 OnPause 返回 NullPointerException

java - 如何让消息出现在不同的侧面?

javascript - 如何为动态添加的跨度元素分配点击事件?

java - 无法让我的 Java 服务器接受来自客户端的文件传输

html - CSS 字体大小细节

python - 使用 BeautifulSoup 提取 HTML 注释之间的文本

java - 读取网站的 HTML 代码

python - 如何在 Python 中使用 lxml 从网页查找所有(完整)子链接

javascript - 为 AngularJS 导航编写自定义函数