用Jsoup在獲取一些網站的數據時,起初獲取很順利,可是在訪問某浪的數據是Jsoup報錯,應該是請求頭裏面的請求類型(ContextType)不符合要求。javascript
請求代碼以下:html
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); }
能看到我這裏設置了請求代理和相應時間。java
報錯信息以下:app
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)ide
修改後代碼:網站
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的檢查google