//这个函数的目的是在获取页面的同时,也获取链接对应的cookie
public static HtmlPage getCookieAndHtml(String url)throws IOException{
URL link=new URL(url);
WebClient wc=new WebClient();
WebRequest request=new WebRequest(link);
wc.getCookieManager().setCookiesEnabled(true);//开启cookie管理
wc.getOptions().setJavaScriptEnabled(true);//开启js解析。对于变态网页,这个是必须的
wc.getOptions().setCssEnabled(true);//开启css解析。对于变态网页,这个是必须的。
HtmlPage page = wc.getPage(request);
CookieManager CM = wc.getCookieManager(); //WC = Your WebClient's name
ThreeExecute.cookie = CM.getCookies();//返回的Cookie在这里,下次请求的时候可能可以用上啦。这里的cookie是ThreeExecute这个类中的全局静态变量,类型为Set<Cookie>
wc.close();
return page;
} //调用
public static HtmlPage getHtml1(String url, Set<Cookie> cookies)throws IOException{
URL link =new URL(url);
final WebClient webClient = new WebClient();
WebRequest request = new WebRequest(link);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(true);
Iterator<Cookie> it = cookies.iterator();
while (it.hasNext()) {
webClient.getCookieManager().addCookie(it.next());
}
final HtmlPage page = webClient.getPage(request);
webClient.close();
return page;
} Jsoup在爬取数据需要用到Cookie时的操作就很简单
Map<String, String> cookies = null;
Response res = Jsoup.connect("http://www.chengmi.com/shanghai").timeout(30000).execute();
cookies = res.cookies();
Document doc = null;
doc = Jsoup.connect(url).cookies(cookies).timeout(30000).get();

最新文章

  1. 开源一个跨平台运行的服务插件 - TaskCore.MainForm
  2. git用法之[回滚代码]
  3. java spring mvc完整包下载地址
  4. mac下配置openCV
  5. NOIP2005过河[DP 状态压缩]
  6. Repaint轨迹保留?(待处理,待编辑)
  7. log4net日志功能使用
  8. 通过IP的方式建立PSSession
  9. GPU优化方法[转]
  10. [HDOJ3466]Proud Merchants(贪心+01背包)
  11. Database and models
  12. Tomcat启动报错 Failed to start component [StandardServer[8005]]解决
  13. Nginx禁止通过IP,未绑定域名访问服务器
  14. SWIFT国际资金清算系统
  15. 有关extern的用法
  16. scu - 3254 - Rain and Fgj(最小点权割)
  17. JAVA课程设计
  18. javaCountDownLatch闭锁
  19. 如果nginx启动失败,错误解决
  20. 急!!ftp登录错误,提示 530 not logged in,连接失败 ,,是怎么回事啊

热门文章

  1. Python_List对象内置方法详解
  2. CentOS定时备份MySQL数据库
  3. Unity Shader 基础
  4. 应用安全 - 免杀 - 工具 - the-backdoor-factory - 使用|命令 - 汇总
  5. 数组被遗忘的内置对象--》Array.find()
  6. Maven使用基础
  7. 模板中用url_for的好处
  8. Java实验报告四
  9. GITFLOW流程
  10. 洛谷 P1801 黑匣子 题解