【问题标题】:JSoup not following redirects and not getting all content for some URLsJSoup 未遵循重定向,也未获取某些 URL 的所有内容
【发布时间】:2014-07-28 19:28:03
【问题描述】:

Android 上的 JSoup 未遵循重定向和/或未获取所有内容,即使我使用 mobile.twitter.com 也是如此。我知道我可能只是不知道自己做错了什么。

我试过的网址是

@987654321@
https://twitter.com/BillGates/status/493831856781926402
private Document getWebPage(String url) {
        Document doc = null;        
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "Received site: " + doc.html()); // TODO delete this debug line when done
        return doc;
    }

【问题讨论】:

  • 你到底想完成什么
  • 在推文中查找图片(如果存在)
  • 我建议不要为此使用 jsoup,因为 twittter 有自己的 api 来处理这样的事情,所以我建议不要使用它,因为它的工作量将超过其价值
  • 我不能这样做,因为像 HTML Viewer 这样的应用程序遵循 URL,如果我使用 Twitter API,我不得不担心配额。 Instagram 等其他网站也有同样的重定向问题。
  • 你试过设置用户代理吗?

标签: java android twitter jsoup


【解决方案1】:

正如 Daniel B 所说,找出正确的 UserAgent 以正确跟踪重定向/从 URL 获取所有内容。

我用过的最好的 UserAgent 如下。

Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2").get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 2016-03-23
    • 2017-01-06
    • 2012-11-04
    • 2023-04-07
    • 2013-02-12
    • 2020-11-29
    相关资源
    最近更新 更多