2015-12-21 69 views
1

我需要下载Instagram资料的HTML源代码并解析它以获取一些信息(媒体和后续计数)。 这是我的代码(它适用于我测试的所有站点,除了Instagram的):如何在我的应用程序中下载Instagram页面的HTML源代码

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调试它,弦乐来源是空的。

+0

你可以张贴链接 – Mohit

+1

首先“把httpclinet拿走并使用改造或凌空” –

+0

@sadeghsaati你在哪里看到'HttpClient'这里? – Henry

回答

2

使用Jsoup进行HTML解析。这是非常容易和方便的。 就拿从这个答案开始,并按照文件link

+0

好的,但为什么我的代码可以与我测试的所有网站一起使用,并且不适用于Instagram? – genialFactory

+0

你的代码看起来也不错。但是Jsoup非常简单。只需一行代码,你就拥有了所有的HTML。试试看。我已经完成了它的简单 –

+0

它为我工作!非常感谢你!! – genialFactory

相关问题