I am using this to get items from this website and return to a list.
Document doc = null;
try {
doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get all td's that are a child of a row - each game 开发者_Python百科has 4 of these
Elements games = doc.select("tr> td.indexList1, tr > td.indexList2");
// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// ...It
while (postIt.hasNext()) {
// Add the game text to the ArrayList
String name = postIt.next().text();
String platform = postIt.next().text();
String genre = postIt.next().text();
String releaseDate = postIt.next().text();
gameList.add(new GameRelease(name, platform, genre, releaseDate));
Log.v(TAG, name +platform + genre +releaseDate);
}
This is the html for the each item
<tr>
<td class="indexList1" align="left">
<a href="http://pc.gamespy.com/pc/hacker-evolution-duality-/" class="b1">
<em>Hacker Evolution Duality </em>
</a>
</td>
<td class="indexList1" align="center">
PC
</td>
<td class="indexList1" align="center">
Adventure
</td>
<td class="indexList1" align="center">
August 15, 2011
<!--08/15/2011-->
</td>
Each item has the same pattern but i want to know could i retreive the url for each item too. You guys may need to view the source of the html too get a better idea.
But i want to store the url for each item in a string.
while (postIt.hasNext()) {
// Get the title of the game
Element title = postIt.next();
System.out.println(title.text());
// Get the anchor element
Element url = title.select("a").first();
// Get the URL here @@@
System.out.println(url.attr("href"));
// Unneeded elements
Element platform = postIt.next();
Element genre = postIt.next();
// Get the release date of the game
Element release = postIt.next();
System.out.println(release.text() + "\n@@@@@@");
}
EDIT: In your case:
Element name = postIt.next();
String nameString = name.text();
Element url = name.select("a").first();
String urlString = url.attr("href");
Each item has the same pattern but i want to know could i retreive the url for each item too.
Elements links = doc.getElementsByTag("a"); // or getElementsByClass("b1");
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
String linkHref = link.attr("href");
}
}
精彩评论