我正在尝试www.pandora.com/profile/stations/olin_d_kirkland
使用 Java 下载 HTML,以匹配我从 Chrome 网页的上下文菜单中选择“查看页面源代码”时得到的内容。
现在,我知道如何用 Java 下载网页 HTML 源代码了。我已经使用 downloads.nl 完成了它并在其他站点上对其进行了测试。然而,潘多拉是一个谜。我的最终目标是从 Pandora 帐户中解析“站”。
具体来说,我想从诸如www.pandora.com/profile/stations/olin_d_kirkland
我曾尝试在 Java 中使用 selenium 库和内置的 URL getter,但是当我应该得到 5300 行时,我只得到了大约 4700 行代码。更不用说代码中没有个性化数据,这就是我的米找。
我想这是因为我没有抓住 JavaScript 或让 JavaScript 先执行,但即使我等待它加载到我的代码中,我也只会得到相同的结果。
如果可能的话,我应该有一个名为“grabPageSource()”的方法,它返回一个字符串。它应该在调用时返回源代码。
public class PandoraStationFinder {
public static void main(String[] args) throws IOException, InterruptedException {
String s = grabPageSource();
String[] lines = s.split("\n\r");
String t;
ArrayList stations = new ArrayList();
for (int i = 0; i < lines.length; i++) {
t = lines[i].trim();
Pattern p = Pattern.compile("<a href=\"/station/\\d+\">[\\w\\s]+</a>");
Matcher m = p.matcher(t);
if (m.matches() ? true : false) {
Station someStation = new Station(t);
stations.add(someStation);
// System.out.println("I found a match on line " + i + ".");
// System.out.println(t);
}
}
}
public static String grabPageSource() throws IOException {
String fullTxt = "";
// Get HTML from www.pandora.com/profile/stations/olin_d_kirkland
return fullTxt;
}
}
它是如何完成的无关紧要,但我想在最终产品中获取一个完整的列表,其中包含 Pandora 上用户喜欢的所有歌曲。