在我的应用程序中,我需要下载 Instagram 配置文件的 HTML 源代码并对其进行解析以获取一些信息(媒体和后续计数)。 这是我的代码(它适用于我测试过的所有网站,Instagram 除外):
try {
InputStream in;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if(!(conn instanceof HttpURLConnection))
throw new NoConnectionException("not instanceof http");
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
in = httpConn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
String source = "";
while((line = br.readLine()) != null)
source += line;
br.close();
} catch(Exception e) {}
当我用 LogCat 调试它时,String source 是空的。
最佳答案
使用 Jsoup 进行 HTML 解析。它非常简单和方便。 从这个答案开始并遵循文档 link
关于android - 如何下载 Instagram 页面的 HTML 源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397346/