Jsoup.connect无法获取正确的HTML内容 [英] Jsoup.connect cannot get correct html contents
问题描述
我使用Jsoup从网站中提取指定的数据,
i use Jsoup to extract specified data from a website,
try{
Document doc = Jsoup.connect("http://example/search/").get();
} catch(IOException){
System.out.println("error");
}
但是我失败了,输出为错误".
but i'm got failed, and the output is "error".
当我使用Mozilla或其他浏览器浏览时,此地址已成功加载. 有什么主意吗?请帮帮我.
when i browse with Mozilla,or another browser this address is successfully to load. Any idea?Please help me..
最诚挚的问候
推荐答案
如果显示IOException
消息中的异常消息,您将看到
If you display the exception message from your IOException
message, you will see
org.jsoup.HttpStatusException:提取URL时发生HTTP错误.状态= 500,网址= ...
org.jsoup.HttpStatusException: HTTP error fetching URL. Status=500, URL=...
解决方案:您需要将用户代理设置为与移动网站相对应
Solution: You need to set the user agent to correspond to the mobile website
Document doc =
Jsoup.connect("http://m.tokobagus.com/search/province").userAgent
("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko)
Chrome/15.0.874.120 Safari/535.2").get();
更重要的是,请记住显示那些异常消息:
More importantly, remember to display those exception messages:
} catch(IOException ioe){
ioe.printStacktrace();
}
这篇关于Jsoup.connect无法获取正确的HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!