在我的应用程序中,我需要下载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源为空。
使用Jsoup进行HTML解析。它非常简单方便。从这个答案开始,按照文档链接