Java爬虫:一个框架就够了
想要爬取某宝的商品,如果只是用HttpURLConnection发个请求,失败率是很高的。一般想要保证成功率的话,都会选择真实的浏览器去抓取。
以前常用的解决方案是selenium或phantomjs,但是它两的环境配置太麻烦了,对程序员极度不友好,自从谷歌推出Puppeteer后,puppeteer迅速流行起来,获得大家一致称赞。它是一个NodeJS库,但今天并不是要使用它来爬取某宝商品,而是使用Java语言写的Jvppeteer,Jvppeteer与Puppeteer是同样的实现原理。
Jvppeteer通过[DevTools](https://chromedevtools.github.io/devtools-protocol/)控制 Chromium 或 Chrome。
默认情况下,以headless模式运行,也可以通过配置运行\’有头\’模式。
你可以在浏览器中手动执行的绝大多数操作都可以使用 Jvppeteer 来完成! 下面是一些示例:
- – 生成页面 PDF。
- – 抓取 SPA(单页应用)并生成预渲J染内容(即“SSR”(服务器端渲染))。
- – 自动提交表单,进行 UI 测试,键盘输入等。
- – 创建一个时时更新的自动化测试环境。 使用最新的 JavaScript 和浏览器功能直接在最新版本的Chrome中执行测试。
- – 捕获网站的 [timeline trace](https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference),用来帮助分析性能问题。
- – 测试浏览器扩展。
开始使用
启动浏览器
//设置基本的启动配置,这里选择了‘有头’模式启动 LaunchOptions options = new OptionsBuilder().withHeadless(false).withExecutablePath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").build(); //启动 Puppeteer.launch(options);
导航到某个页面
package com.ruiyun.example; import com.ruiyun.jvppeteer.core.Puppeteer; import com.ruiyun.jvppeteer.core.browser.Browser; import com.ruiyun.jvppeteer.options.LaunchOptions; import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder; import com.ruiyun.jvppeteer.core.page.Page; import java.util.ArrayList; public class PageGotoExample { public static void main(String[] args) throws Exception { // String path = new String("F:\\java教程\\49期\\vuejs\\puppeteer\\.local-chromium\\win64-722234\\chrome-win\\chrome.exe".getBytes(),"GBK"); String path ="D:\\develop\\project\\toString\\chrome-win\\chrome.exe"; ArrayList<String> arrayList = new ArrayList<>(); LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).withExecutablePath(path).build(); arrayList.add("--no-sandbox"); arrayList.add("--disable-setuid-sandbox"); Browser browser = Puppeteer.launch(options); Page page = browser.newPage(); page.goTo("https://www.taobao.com/about/"); browser.close(); } }
生成页面PDF
package com.ruiyun.example; import com.ruiyun.jvppeteer.core.Puppeteer; import com.ruiyun.jvppeteer.core.browser.Browser; import com.ruiyun.jvppeteer.core.page.Page; import com.ruiyun.jvppeteer.options.LaunchOptions; import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder; import java.util.ArrayList; public class PagePDFExample2 { public static void main(String[] args) throws Exception { //String path = new String("F:\\java教程\\49期\\vuejs\\puppeteer\\.local-chromium\\win64-722234\\chrome-win\\chrome.exe".getBytes(),"UTF-8"); ArrayList<String> arrayList = new ArrayList<>(); String path = "D:\\develop\\project\\toString\\chrome-win\\chrome.exe"; //生成pdf必须在无厘头模式下才能生效 LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).withExecutablePath(path).build(); arrayList.add("--no-sandbox"); arrayList.add("--disable-setuid-sandbox"); Browser browser = Puppeteer.launch(options); Page page = browser.newPage(); page.goTo("https://www.baidu.com/?tn=98012088_10_dg&ch=3"); page.pdf("/tmp/test.pdf"); } }
页面截图
package com.ruiyun.example; import com.ruiyun.jvppeteer.core.Puppeteer; import com.ruiyun.jvppeteer.core.browser.Browser; import com.ruiyun.jvppeteer.core.page.Page; import com.ruiyun.jvppeteer.options.Clip; import com.ruiyun.jvppeteer.options.LaunchOptions; import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder; import com.ruiyun.jvppeteer.options.ScreenshotOptions; import java.util.ArrayList; public class PagescreenshotExample { public static void main(String[] args) throws Exception { // String path = new String("F:\\java教程\\49期\\vuejs\\puppeteer\\.local-chromium\\win64-722234\\chrome-win\\chrome.exe".getBytes(),"UTF-8"); ArrayList<String> arrayList = new ArrayList<>(); String path = "D:\\develop\\project\\toString\\chrome-win\\chrome.exe"; LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).withExecutablePath(path).build(); arrayList.add("--no-sandbox"); arrayList.add("--disable-setuid-sandbox"); Browser browser = Puppeteer.launch(options); Page page = browser.newPage(); page.goTo("https://www.baidu.com/?tn=98012088_10_dg&ch=3"); // ScreenshotOptions screenshotOptions = new ScreenshotOptions(); // //设置截图范围 // Clip clip = new Clip(1.0,1.56,400,400); // screenshotOptions.setClip(clip); // //设置存放的路径 // screenshotOptions.setPath("test.png"); // page.screenshot(screenshotOptions); ScreenshotOptions screenshotOptions = new ScreenshotOptions(); //设置截图范围 Clip clip = new Clip(1.0,1.56,400,400); screenshotOptions.setClip(clip); //设置存放的路径 screenshotOptions.setPath("test.png"); page.screenshot(screenshotOptions); } }
页面性能分析
package com.ruiyun.example; import com.ruiyun.jvppeteer.core.Puppeteer; import com.ruiyun.jvppeteer.core.browser.Browser; import com.ruiyun.jvppeteer.core.page.Page; import com.ruiyun.jvppeteer.options.LaunchOptions; import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder; import java.util.ArrayList; public class PageTracingExample { public static void main(String[] args) throws Exception { //String path = new String("F:\\java教程\\49期\\vuejs\\puppeteer\\.local-chromium\\win64-722234\\chrome-win\\chrome.exe".getBytes(),"UTF-8"); ArrayList<String> arrayList = new ArrayList<>(); String path = "D:\\develop\\project\\toString\\chrome-win\\chrome.exe"; LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).withExecutablePath(path).build(); arrayList.add("--no-sandbox"); arrayList.add("--disable-setuid-sandbox"); Browser browser = Puppeteer.launch(options); Page page = browser.newPage(); //开启追踪 page.tracing().start("C:\\Users\\howay\\Desktop\\trace.json"); page.goTo("https://www.baidu.com/?tn=98012088_10_dg&ch=3"); page.tracing().stop(); //waifor tracingComplete } }
Jvppeteer的项目源码:https://github.com/fanyong920/jvppeteer