【问题标题】:How to download HTML source of an Instagram page如何下载 Instagram 页面的 HTML 源代码
【发布时间】:2015-12-21 13:55:01
【问题描述】:

在我的应用程序中,我需要下载 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 调试它时,字符串源是空的。

【问题讨论】:

  • 首先“把httpclinet收起来,用Retrofit或者Volley”
  • @sadeghsaati 你在哪里看到HttpClient
  • 也许您检索到异常?你的 catch 块是空的,所以它可能是失败的。
  • @Henry 对不起,我的意思是 HttpURLConnection

标签: android instagram


【解决方案1】:

使用 Jsoup 进行 HTML 解析。这非常容易和方便。 从这个答案开始,关注文档link

【讨论】:

  • 好的,但是为什么我的代码适用于我测试过的所有网站,却不适用于 Instagram?
  • 你的代码看起来也不错。但是 Jsoup 非常简单。只需一行代码,您就拥有了所有 HTML。试试看。我已经做到了,它很简单
猜你喜欢
  • 2019-10-06
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 2010-11-24
  • 1970-01-01
  • 2010-10-10
相关资源
最近更新 更多