需要将元素从网络传输到数组列表

发布于 2024-12-28 23:19:32 字数 6712 浏览 1 评论 0原文

在成功抓取网络上的相关数据后,我需要将其传输到 ArrayList 中。

正如你在下面看到的,我打印了数据。现在我需要做的就是将其放入 ArrayList 中。有什么想法吗?

代码:

static String html = "http://games.espn.go.com/fba/playerrater?&slotCategoryId=1";

public static void main(String[] args) throws IOException, SQLException, InterruptedException {
    Document doc = Jsoup.connect(html).get();
    String title = doc.title();
   System.out.println(title);


    Iterator<Element> trSIter = doc.select("table.playerTableTable").iterator();
    while (trSIter.hasNext()) {
        Element  trEl = trSIter.next().child(0);
        Elements  tdEls = trEl.children();
        Iterator<Element> tdIter = tdEls.select("tr").iterator();
        boolean firstRow = true;
        while (tdIter.hasNext()) {

            Element tr = (Element)tdIter.next();
            if (firstRow) {
                firstRow = false;
                continue;
            }

        while (tdIter.hasNext()) 
        {  

            System.out.println("============================");
            Element tdEl = tdIter.next();
            String name = 
                    tdEl.getElementsByClass("playertablePlayerName").text();
            System.out.println("Name: "+name);
            String pointStr = 
                    tdEl.getElementsByClass("sortedCell").text();
            System.out.println("points: "+pointStr);
            //System.out.println(tdEl);
            Elements tdsEls = tdEl.select("td.playertableData");
            Iterator<Element> columnIt = tdsEls.iterator();
            boolean firstRow1 = true;


            while(columnIt.hasNext())
            {


                Element column = columnIt.next();
                 String stat1 =column.text();
                System.out.print(column.className()+":"+column.text()+","); 

输出:

Name: Kobe Bryant, LAL SG
points: 14.68
playertableData:1,playertableData:0.62,playertableData:2.37,playertableData:1.36,playertableData:1.20,playertableData:2.66,playertableData:1.73,playertableData:0.15,playertableData:4.59,playertableData sortedCell:14.68,============================
Name: Joe Johnson, Atl SG, SF
points: 10.04
playertableData:2,playertableData:-0.58,playertableData:2.05,playertableData:3.27,playertableData:0.46,playertableData:1.41,playertableData:0.97,playertableData:0.01,playertableData:2.44,playertableData sortedCell:10.04,============================
Name: James Harden, OKC SG
points: 9.28
playertableData:3,playertableData:0.73,playertableData:2.48,playertableData:2.53,playertableData:0.53,playertableData:1.00,playertableData:0.46,playertableData:-0.25,playertableData:1.81,playertableData sortedCell:9.28,============================
Name: Andre Iguodala, Phi SF, SG
points: 8.74
playertableData:4,playertableData:0.42,playertableData:-1.47,playertableData:1.57,playertableData:1.31,playertableData:1.90,playertableData:3.13,playertableData:0.68,playertableData:1.19,playertableData sortedCell:8.74,============================
Name: Monta Ellis, GS PG, SG
points: 8.38
playertableData:5,playertableData:-0.58,playertableData:0.77,playertableData:1.15,playertableData:-0.14,playertableData:2.73,playertableData:2.11,playertableData:-0.12,playertableData:2.46,playertableData sortedCell:8.38,============================
Name: Kevin Martin, Hou SG
points: 7.87
playertableData:6,playertableData:-0.91,playertableData:3.01,playertableData:2.85,playertableData:0.02,playertableData:0.79,playertableData:0.46,playertableData:-0.66,playertableData:2.31,playertableData sortedCell:7.87,============================
Name: Jarrett Jack, Nor PG, SG
points: 7.67
playertableData:7,playertableData:0.15,playertableData:1.49,playertableData:0.51,playertableData:0.44,playertableData:3.25,playertableData:0.33,playertableData:-0.12,playertableData:1.62,playertableData sortedCell:7.67,============================
Name: Jason Terry, Dal SG
points: 7.31
playertableData:8,playertableData:-0.89,playertableData:0.77,playertableData:3.27,playertableData:-0.53,playertableData:1.41,playertableData:2.24,playertableData:-0.39,playertableData:1.42,playertableData sortedCell:7.31,============================
Name: Luke Ridnour, Min PG, SG
points: 6.21
playertableData:9,playertableData:1.05,playertableData:0.40,playertableData:1.57,playertableData:-0.37,playertableData:1.38,playertableData:1.22,playertableData:-0.12,playertableData:1.08,playertableData sortedCell:6.21,============================
Name: Ray Allen, Bos SG  DTD
points: 6.19
playertableData:10,playertableData:1.21,playertableData:1.35,playertableData:3.06,playertableData:-0.16,playertableData:0.31,playertableData:0.20,playertableData:-0.79,playertableData:1.01,playertableData sortedCell:6.19,============================
Name: Tyreke Evans, Sac PG, SG
points: 6.05
playertableData:11,playertableData:-1.04,playertableData:0.05,playertableData:0.19,playertableData:0.94,playertableData:1.90,playertableData:1.98,playertableData:0.15,playertableData:1.87,playertableData sortedCell:6.05,============================
Name: D.J. Augustin, Cha PG, SG  DTD
points: 5.88
playertableData:12,playertableData:-0.93,playertableData:0.73,playertableData:2.00,playertableData:0.07,playertableData:3.07,playertableData:0.33,playertableData:-0.66,playertableData:1.26,playertableData sortedCell:5.88,============================
Name: Wesley Matthews, Por SG, SF
points: 5.43
playertableData:13,playertableData:-0.80,playertableData:1.14,playertableData:2.32,playertableData:0.14,playertableData:-0.04,playertableData:1.86,playertableData:-0.39,playertableData:1.21,playertableData sortedCell:5.43,============================
Name: Brandon Knight, Det PG, SG
points: 5.38
playertableData:14,playertableData:-0.31,playertableData:0.46,playertableData:2.10,playertableData:0.30,playertableData:1.17,playertableData:0.84,playertableData:-0.39,playertableData:1.20,playertableData sortedCell:5.38,============================
Name: Paul George, Ind SF, SG
points: 5.37
playertableData:15,playertableData:0.04,playertableData:0.58,playertableData:1.79,playertableData:0.64,playertableData:0.38,playertableData:1.09,playertableData:0.28,playertableData:0.57,playertableData sortedCell:5.37,============================
Name: MarShon Brooks, NJ SG, SF  DTD
points: 5.22
playertableData:16,playertableData:0.67,playertableData:0.34,playertableData:1.26,playertableData:0.53,playertableData:0.03,playertableData:0.97,playertableData:0.01,playertableData:1.41,playertableData sortedCell:5.22,============================
Name: Paul Pierce, Bos SF, SG
points: 5.20

After successfully scraping the web with the relevent data I need to transfer it to an ArrayList.

As you can see below I printed out the data. Now all I need to do is put it in an ArrayList. Any idea?

Code:

static String html = "http://games.espn.go.com/fba/playerrater?&slotCategoryId=1";

public static void main(String[] args) throws IOException, SQLException, InterruptedException {
    Document doc = Jsoup.connect(html).get();
    String title = doc.title();
   System.out.println(title);


    Iterator<Element> trSIter = doc.select("table.playerTableTable").iterator();
    while (trSIter.hasNext()) {
        Element  trEl = trSIter.next().child(0);
        Elements  tdEls = trEl.children();
        Iterator<Element> tdIter = tdEls.select("tr").iterator();
        boolean firstRow = true;
        while (tdIter.hasNext()) {

            Element tr = (Element)tdIter.next();
            if (firstRow) {
                firstRow = false;
                continue;
            }

        while (tdIter.hasNext()) 
        {  

            System.out.println("============================");
            Element tdEl = tdIter.next();
            String name = 
                    tdEl.getElementsByClass("playertablePlayerName").text();
            System.out.println("Name: "+name);
            String pointStr = 
                    tdEl.getElementsByClass("sortedCell").text();
            System.out.println("points: "+pointStr);
            //System.out.println(tdEl);
            Elements tdsEls = tdEl.select("td.playertableData");
            Iterator<Element> columnIt = tdsEls.iterator();
            boolean firstRow1 = true;


            while(columnIt.hasNext())
            {


                Element column = columnIt.next();
                 String stat1 =column.text();
                System.out.print(column.className()+":"+column.text()+","); 

OUTPUT:

Name: Kobe Bryant, LAL SG
points: 14.68
playertableData:1,playertableData:0.62,playertableData:2.37,playertableData:1.36,playertableData:1.20,playertableData:2.66,playertableData:1.73,playertableData:0.15,playertableData:4.59,playertableData sortedCell:14.68,============================
Name: Joe Johnson, Atl SG, SF
points: 10.04
playertableData:2,playertableData:-0.58,playertableData:2.05,playertableData:3.27,playertableData:0.46,playertableData:1.41,playertableData:0.97,playertableData:0.01,playertableData:2.44,playertableData sortedCell:10.04,============================
Name: James Harden, OKC SG
points: 9.28
playertableData:3,playertableData:0.73,playertableData:2.48,playertableData:2.53,playertableData:0.53,playertableData:1.00,playertableData:0.46,playertableData:-0.25,playertableData:1.81,playertableData sortedCell:9.28,============================
Name: Andre Iguodala, Phi SF, SG
points: 8.74
playertableData:4,playertableData:0.42,playertableData:-1.47,playertableData:1.57,playertableData:1.31,playertableData:1.90,playertableData:3.13,playertableData:0.68,playertableData:1.19,playertableData sortedCell:8.74,============================
Name: Monta Ellis, GS PG, SG
points: 8.38
playertableData:5,playertableData:-0.58,playertableData:0.77,playertableData:1.15,playertableData:-0.14,playertableData:2.73,playertableData:2.11,playertableData:-0.12,playertableData:2.46,playertableData sortedCell:8.38,============================
Name: Kevin Martin, Hou SG
points: 7.87
playertableData:6,playertableData:-0.91,playertableData:3.01,playertableData:2.85,playertableData:0.02,playertableData:0.79,playertableData:0.46,playertableData:-0.66,playertableData:2.31,playertableData sortedCell:7.87,============================
Name: Jarrett Jack, Nor PG, SG
points: 7.67
playertableData:7,playertableData:0.15,playertableData:1.49,playertableData:0.51,playertableData:0.44,playertableData:3.25,playertableData:0.33,playertableData:-0.12,playertableData:1.62,playertableData sortedCell:7.67,============================
Name: Jason Terry, Dal SG
points: 7.31
playertableData:8,playertableData:-0.89,playertableData:0.77,playertableData:3.27,playertableData:-0.53,playertableData:1.41,playertableData:2.24,playertableData:-0.39,playertableData:1.42,playertableData sortedCell:7.31,============================
Name: Luke Ridnour, Min PG, SG
points: 6.21
playertableData:9,playertableData:1.05,playertableData:0.40,playertableData:1.57,playertableData:-0.37,playertableData:1.38,playertableData:1.22,playertableData:-0.12,playertableData:1.08,playertableData sortedCell:6.21,============================
Name: Ray Allen, Bos SG  DTD
points: 6.19
playertableData:10,playertableData:1.21,playertableData:1.35,playertableData:3.06,playertableData:-0.16,playertableData:0.31,playertableData:0.20,playertableData:-0.79,playertableData:1.01,playertableData sortedCell:6.19,============================
Name: Tyreke Evans, Sac PG, SG
points: 6.05
playertableData:11,playertableData:-1.04,playertableData:0.05,playertableData:0.19,playertableData:0.94,playertableData:1.90,playertableData:1.98,playertableData:0.15,playertableData:1.87,playertableData sortedCell:6.05,============================
Name: D.J. Augustin, Cha PG, SG  DTD
points: 5.88
playertableData:12,playertableData:-0.93,playertableData:0.73,playertableData:2.00,playertableData:0.07,playertableData:3.07,playertableData:0.33,playertableData:-0.66,playertableData:1.26,playertableData sortedCell:5.88,============================
Name: Wesley Matthews, Por SG, SF
points: 5.43
playertableData:13,playertableData:-0.80,playertableData:1.14,playertableData:2.32,playertableData:0.14,playertableData:-0.04,playertableData:1.86,playertableData:-0.39,playertableData:1.21,playertableData sortedCell:5.43,============================
Name: Brandon Knight, Det PG, SG
points: 5.38
playertableData:14,playertableData:-0.31,playertableData:0.46,playertableData:2.10,playertableData:0.30,playertableData:1.17,playertableData:0.84,playertableData:-0.39,playertableData:1.20,playertableData sortedCell:5.38,============================
Name: Paul George, Ind SF, SG
points: 5.37
playertableData:15,playertableData:0.04,playertableData:0.58,playertableData:1.79,playertableData:0.64,playertableData:0.38,playertableData:1.09,playertableData:0.28,playertableData:0.57,playertableData sortedCell:5.37,============================
Name: MarShon Brooks, NJ SG, SF  DTD
points: 5.22
playertableData:16,playertableData:0.67,playertableData:0.34,playertableData:1.26,playertableData:0.53,playertableData:0.03,playertableData:0.97,playertableData:0.01,playertableData:1.41,playertableData sortedCell:5.22,============================
Name: Paul Pierce, Bos SF, SG
points: 5.20

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2025-01-04 23:19:32

创建一个包含名称、点数等变量的 Player 类。然后为每个玩家创建一个对象,最后将其添加到 ArrayList 中。

Player currentPlayer;
ArrayList<Player> players = new ArrayList<Player>();
// a lot of your code
while (tdIter.hasNext()) {  
    System.out.println("============================");
    Element tdEl = tdIter.next();
    currentPlayer = new Player();
    // instead of that:
    //String name = tdEl.getElementsByClass("playertablePlayerName").text();
    // do that:
    currentPlayer.mName = tdEl.getElementsByClass("playertablePlayerName").text();
    // the same with the rest of the gathered information
    // at the last line in this while loop
    players.add(currentPlayer);
}

Make a class Player containing variables for name, points etc. Then create an object per player, finally add this to an ArrayList<Player>.

Player currentPlayer;
ArrayList<Player> players = new ArrayList<Player>();
// a lot of your code
while (tdIter.hasNext()) {  
    System.out.println("============================");
    Element tdEl = tdIter.next();
    currentPlayer = new Player();
    // instead of that:
    //String name = tdEl.getElementsByClass("playertablePlayerName").text();
    // do that:
    currentPlayer.mName = tdEl.getElementsByClass("playertablePlayerName").text();
    // the same with the rest of the gathered information
    // at the last line in this while loop
    players.add(currentPlayer);
}
怀念你的温柔 2025-01-04 23:19:32

谢谢沃伦!

我添加了一个开关以遍历所有元素
希望有些人能从我的问题中受益。感谢您的宝贵时间。

switch (tdCount++) {

                    case 1:
                        stat1 = Double.parseDouble(column.text());
                        break;
                    case 2:
                        stat2 = Double.parseDouble(column.text());
                        break;
                    case 3:
                        stat3 = Double.parseDouble(column.text());
                        break;
                    case 4:
                        stat4 = Double.parseDouble(column.text());
                        break;
                    case 5:
                        stat5 = Double.parseDouble(column.text());
                        break;
                    case 6:
                        stat6 = Double.parseDouble(column.text());
                        break;
                    case 7:
                        stat7 = Double.parseDouble(column.text());
                        break;
                    case 8:
                        stat8 = Double.parseDouble(column.text());
                        break;
                    case 9:
                        stat9 = Double.parseDouble(column.text());
                        break;
                    case 10:
                        stat10 = Double.parseDouble(column.text());
                        break;

                    }
                }

                PlayerBean pb = new PlayerBean(name, stat1, stat2, stat3,
                        stat4, stat5, stat6, stat7, stat8, stat9, stat10);
                playerlist.add(pb);
            }

Thanks Warren!

I added a switch in order to go thru all of the elemnts
Hope some people can benifit from my question.Thanks for your time.

switch (tdCount++) {

                    case 1:
                        stat1 = Double.parseDouble(column.text());
                        break;
                    case 2:
                        stat2 = Double.parseDouble(column.text());
                        break;
                    case 3:
                        stat3 = Double.parseDouble(column.text());
                        break;
                    case 4:
                        stat4 = Double.parseDouble(column.text());
                        break;
                    case 5:
                        stat5 = Double.parseDouble(column.text());
                        break;
                    case 6:
                        stat6 = Double.parseDouble(column.text());
                        break;
                    case 7:
                        stat7 = Double.parseDouble(column.text());
                        break;
                    case 8:
                        stat8 = Double.parseDouble(column.text());
                        break;
                    case 9:
                        stat9 = Double.parseDouble(column.text());
                        break;
                    case 10:
                        stat10 = Double.parseDouble(column.text());
                        break;

                    }
                }

                PlayerBean pb = new PlayerBean(name, stat1, stat2, stat3,
                        stat4, stat5, stat6, stat7, stat8, stat9, stat10);
                playerlist.add(pb);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文