如何在Java上使用html代码? [英] How to work with html code readed on Java?

查看:121
本文介绍了如何在Java上使用html代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何阅读网站的html代码,例如,下一个java代码读取中的所有html代码http://www.transfermarkt.co.uk/en/fc- barcelona / startseite / verein_131.html 这是一个网站,显示FC的所有足球运动员巴塞罗那。

  import java.io.BufferedReader; 
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadWebPage {
public static void main(String [] args)throws IOException {
String urltext =http://www.transfermarkt.co.uk/en /fc-barcelona/startseite/verein_131.html;
网址url =新网址(urltext);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream()));
String inputLine; ((inputLine = in.readLine())!= null){
//处理每一行,


System.out.println(inputLine);
}
in.close();


$ / code>

好的,但现在我需要使用HTML代码,我需要获得球队每个球员的名字(Valdés,Victor,Pinto,JoséManuel等等)和位置(守门员,防守,中场,前锋)。例如,我需要创建一个 ArrayList< String> PlayerNames 和一个 ArrayList< String> PlayerPositions ,并把这些数组放在所有球员的名字和位置上。



我该怎么做?我无法找到可以在Google上执行的代码示例..... 欢迎使用代码示例

谢谢

解决方案

我会推荐使用 HtmlUnit ,它可以让你访问HTML页面的DOM树,甚至在数据使用AJAX动态放入页面时执行JavaScript。



你可以也可以使用 JSoup :没有JavaScript,但更轻量,并且支持CSS选择器。


I know how to read the html code of a website, for example, the next java code reads all the html code from http://www.transfermarkt.co.uk/en/fc-barcelona/startseite/verein_131.html this is a website that shows all the football players of F.C. Barcelona.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadWebPage {
    public static void main(String[] args) throws IOException {
        String urltext = "http://www.transfermarkt.co.uk/en/fc-barcelona/startseite/verein_131.html";
        URL url = new URL(urltext);
        BufferedReader in = new BufferedReader(new InputStreamReader(url
                .openStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            // Process each line.
            System.out.println(inputLine);
        }
        in.close();
    }
}

OK, but now i need to work with the HTML code, i need to obtain the names ("Valdés, Victor", "Pinto, José Manuel", etc...) and the positions (Goalkeeper, Defence, Midfield, Striker) of each of the players of the team. For example, i need to create a ArrayList <String> PlayerNames and a ArrayList <String> PlayerPositions and put on these arrays all the names and positions of all the players.

How i can do it??? i can't find the code example that can do it on google..... code examples are welcome

thanks

解决方案

I would recommend using HtmlUnit, which will give you access to the DOM tree of the HTML page, and even execute JavaScript in case the data are dynamically put in the page using AJAX.

You could also use JSoup: no JavaScript, but more lightweight and support for CSS selectors.

这篇关于如何在Java上使用html代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆