Jsoup獲取部分頁面數據失敗 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.


  用Jsoup在獲取一些網站的數據時,起初獲取很順利,但是在訪問某浪的數據是Jsoup報錯,應該是請求頭里面的請求類型(ContextType)不符合要求。

  請求代碼如下:

    private static void testOuGuanMatch() throws IOException{
        Document doc = Jsoup.connect("我的URL").userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").timeout(5000).get();
        System.out.println(doc);
    }

  能看到我這里設置了請求代理和相應時間。

  報錯信息如下:

org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/javascript, URL=....
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:472)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
    at calendarSpider.SpiderTest.testOuGuanMatch(SpiderTest.java:174)
    at calendarSpider.SpiderTest.main(SpiderTest.java:39)

  在google上查找到了解決方法:添加ignoreContentType(true)

  修改后代碼:

    private static void testOuGuanMatch() throws IOException{
        Document doc = Jsoup.connect("我的URL").ignoreContentType(true).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").timeout(5000).get();
        System.out.println(doc);
    }

  那這里的ignoreContentType(true)看詞就知道忽略ContextType的檢查

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM