需要将元素从网络传输到数组列表
在成功抓取网络上的相关数据后,我需要将其传输到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个包含名称、点数等变量的
Player
类。然后为每个玩家创建一个对象,最后将其添加到ArrayList
中。Make a class
Player
containing variables for name, points etc. Then create an object per player, finally add this to anArrayList<Player>
.谢谢沃伦!
我添加了一个开关以遍历所有元素
希望有些人能从我的问题中受益。感谢您的宝贵时间。
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.