Parts of the page are loaded with javascript, which is why you can't see the divs you're looking for.
You can use a browser to load the page and interpret the javascript before parsing. A library like webdrivermanager will help.
public static void main(String[] args) throws Exception {
ChromeDriverManager.getInstance().setup();
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.get("https://osu.ppy.sh/u/charless");
Document d = Jsoup.parse(chromeDriver.getPageSource());
chromeDriver.close();
for (Element line : d.select("div.profileStatLine")) {
System.out.println(line.select("b").text());
}
}
The alternative is to examine the javascript in the page and make the same calls that it does to retrieve the data.
The page is loading the profile from https://osu.ppy.sh/pages/include/profile-general.php?u=4084042&m=0
. It looks like u
is simply the user ID, which is relatively simple to extract from the page:
public class ProfileScraper {
private static final Pattern UID_PATTERN = Pattern.compile("var userId = (\\d+);");
public static void main(String[] args) throws IOException {
String uid = getUid("charless");
Document d = Jsoup.connect("https://osu.ppy.sh/pages/include/profile-general.php?u=" + uid).get();
for (Element line : d.select("div.profileStatLine")) {
System.out.println(line.select("b").text());
}
}
public static String getUid(String name) throws IOException {
Document d1 = Jsoup.connect("https://osu.ppy.sh/u/" + name).get();
for (Element script : d1.select("script")) {
String text = script.data();
Matcher uidMatcher = UID_PATTERN.matcher(text);
if (uidMatcher.find()) {
return uidMatcher.group(1);
}
}
throw new IOException("No such character");
}
}