-1
A
回答
0
通過標識和標籤,直到你到了相關的標籤,然後通過屬性讓他們選擇。檢查下面的代碼片段:
Document doc = Jsoup.parse("html_file");
Element loginform = doc.getElementById("search_result_container");
Elements inputElements = loginform.getElementsByTag("div");
Element secondDiv = inputElements.get(1);
Elements hyperLinks = secondDiv.getElementsByTag("a");
for (Element alink : hyperLinks) {
String href = alink.attr("href");
String id = alink.attr("id");
}
0
好的,我做到了。有用!!感謝SUNNYben,你給了我正確的輸入!
這裏是我的解決方案,代碼:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Steam_GameID_Links
{
public static void main(String[] args)
{
Steam_GameID_Links wc = new Steam_GameID_Links();
try
{
String url = "http://store.steampowered.com/search/?sort_by=_ASC&category1=998&page=1";
Document document = Jsoup.connect(url).get();
// nur die Spielnamen
Elements howMuchPages = document.select(".search_pagination_right");
String[] stuff = howMuchPages.text().split(" ");
String tmp = stuff[4].replace(" ", "").replace(".", "");
StringBuilder sb = new StringBuilder();
for(int i = 0; i < tmp.length(); i++)
{
if(Character.isDigit(tmp.charAt(i)))
{
sb.append(tmp.charAt(i));
}
}
String last = sb.toString().trim();;
int lastPages = Integer.parseInt(last);
int counter = 0;
for(int i = 1; i < lastPages + 1; i++)
{
url = "http://store.steampowered.com/search/?sort_by=_ASC&category1=998&page=" + i;
document = Jsoup.connect(url).get();
// waehlt zunaechst den ElternKnoten: <div id="search_result_container">
Element parentNode = document.getElementById("search_result_container");
Elements childNodes = parentNode.getElementsByAttribute("data-ds-appid");
for(Element alink : childNodes)
{
String href = alink.attr("href");
String id = alink.attr("data-ds-appid");
String name = alink.getElementsByClass("title").text();
System.out.println("Spiel: " + name + ", ID: " + id + ", SpieleLink: " + href);
// wc.writeSpielNameIDLink("Spiel: " + name + ", ID: " + id + ", SpieleLink: " + href + "\n");
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
相關問題
- 1. 如何通過jsoup獲取元素對?
- 2. 在JSoup中按類獲取元素
- 3. 如何使用jQuery根據鏈接的href獲取元素ID?
- 4. JSOUP:如何獲得Href?
- 5. 如何在jsoup中獲得元素的一級子元素
- 6. jsoup來獲取特定元素在Tablw
- 7. 如何使用Jsoup在Android中獲取元素的一部分
- 8. 獲取唯一ID和href
- 9. 在JavaScript中獲取元素ID和通用元素
- 10. 如何使用jSoup在一個html類中獲取多個「a href」
- 11. 如何從JSOUP中的元素獲取特定信息?
- 12. 如何從源代碼中獲取元素Jsoup
- 13. 如何在使用Jsoup時獲取元素的LineNumber?
- 14. jsoup:如何獲得第一個元素?
- 15. JSOUP提取多種元素的同時
- 16. 獲取元素多個ID或類
- 17. 如何獲取父元素的ID?
- 18. 如何正確獲取元素的ID?
- 19. 如何獲取周圍元素的ID?
- 20. jquery如何獲取動態元素ID
- 21. 如何獲取HTML元素的ID?
- 22. 如何獲取元素的ID
- 23. Javascript:如何通過ID獲取元素?
- 24. 使用Jsoup獲取網頁元素
- 25. Android JSOUP獲取表格的元素
- 26. 提取HREF值jsoup
- 27. 獲取ID,從HREF
- 28. 在jQuery中通過ID獲取元素
- 29. 在Ember中通過ID獲取元素
- 30. 在視口中獲取元素ID