去哪儿网手机版机票数据

去哪儿网手机版机票数据选型请求参数中 有 pre m 等 需要进行 js 激活成功教程 难度较大 尝试了两次失败了 换方式

目录

携程手机版国内机票数据
携程手机版国际机票数据
携程网页版国内机票数据
携程网页版国际机票数据
去哪儿网手机版机票数据
去哪儿网网页版机票数据
携程手机版机票数据添加代理
去哪儿网网页版机票数据添加代理

选型

请求参数中,有pre、_m_等,需要进行js激活成功教程,难度较大,尝试了两次失败了,换方式。使用selenium抓取网页数据。
首选需要下载chromedriver,根据chrome的版本进行选择。链接地址:http://chromedriver.storage.googleapis.com/index.html
如果选择的版本错误,运行的时候控制台也会打印正确的版本,重新下载替换即可。

概述

Java开发

引入包

 <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> </dependency> <dependency> <groupId>net.lightbody.bmp</groupId> <artifactId>browsermob-core</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version> 22.0</version> </dependency> <dependency> <groupId>net.lightbody.bmp</groupId> <artifactId>browsermob-legacy</artifactId> <version>2.1.5</version> </dependency> 

其中,selenium-java的作用即为动态数据获取,其他三个的作用是为了作为代理,获取到网页上没有显示但是接口返回的数据(版本必须对应,否则运行会报错)。

请求配置

去哪儿网做了反爬虫判断,如果直接打开chrome会一直加载中,不出来数据,所以需要增加下option的配置。

 System.setProperty("webdriver.chrome.driver", "chromedriver的完整路径名称"); ChromeOptions option = new ChromeOptions(); option.addArguments("disable-infobars"); List<Object> list=new ArrayList<>(); list.add("enable-automation"); option.setExperimentalOption("excludeSwitches",list); option.addArguments("--disable-blink-features", "--disable-blink-features=AutomationControlled"); 

添加代理

此步骤可选,代理的作用是可以拦截到所有网络请求,这样接口有但是界面上没有显示的数据也就可以得到了。

 BrowserMobProxy proxy = new BrowserMobProxyServer(); proxy.start(0); proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT); proxy.setHarCaptureTypes(CaptureType.RESPONSE_CONTENT); proxy.newHar("qunanr"); Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy); seleniumProxy.setHttpProxy("localhost:" + proxy.getPort()); seleniumProxy.setSslProxy("localhost:" + proxy.getPort()); proxy.addRequestFilter((request, contents, messageInfo) -> { System.out.println("这里拦截请求" + request.uri() + "" + contents.getTextContents() + ">>>" + request.headers().get("Cookie")); return null; }); option.setProxy(seleniumProxy); option.setAcceptInsecureCerts(true); option.setExperimentalOption("useAutomationExtension", false); proxy.addResponseFilter((response, contents, messageInfo) -> { if (messageInfo.getUrl().contains("wbdflightlist")) { // 这个接口是机票列表详细信息,可解析后使用 System.out.println(response.status() + ":" + response.headers().get("cookie") + ">>>" + contents.getTextContents()); } }); 

获取网页数据

 String url = "https://m.flight.qunar.com/ncs/page/flightlist?depCity=%E4%B8%8A%E6%B5%B7&arrCity=%E5%8C%97%E4%BA%AC&goDate=2022-01-01&from=touch_index_search&child=0&baby=0&cabinType=0"; WebDriver webDriver = new ChromeDriver(option); webDriver.get(url); List<WebElement> resultElements = webDriver.findElements(By.className("list-content")).get(0).findElements(By.tagName("li")); for (int i = 0; i < resultElements.size(); i++) { System.out.println((i+1) + "\n" + resultElements.get(i).getText()); System.out.println(); } proxy.stop(); webDriver.quit(); 

打印内容大概如下,可以根据需要调整标签数据。

19 15:00 虹桥T2 2时15分 17:15 首都T2 东航MU5115 波音777(大) 500 经济舱2.8折 20 15:30 虹桥T2 2时30分 18:00 首都T2 东航MU5161 空客330(大) 500 经济舱2.8折 21 16:00 虹桥T2 2时20分 18:20 首都T2 东航MU5117 波音777(大) 500 经济舱2.8折 22 16:15 浦东T2 2时25分 18:40 首都T3 国航CA1884 空客330(大) 500 经济舱2.8折 23 17:00 虹桥T2 2时15分 19:15 首都T2 东航MU5119 空客330(大) 500 经济舱2.8折 24 18:00 虹桥T2 2时15分 20:15 首都T2 东航MU5121 波音777(大) 500 经济舱2.8折 25 19:00 虹桥T2 2时20分 21:20 首都T2 东航MU5123 空客330(大) 500 经济舱2.8折 

其他

移动端的如果加载更多,会出现输入手机号验证码登录的页面,暂时没有想到处理该问题的方法。

// 加载更多 webDriver.findElements(By.className("list-getmore")).get(0).click(); // 给某个控件赋值,格式是 webDriver.findElements(By.className("list-getmore")).get(0).sendKeys("数值");) 

完整代码

 public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/Users/admin/Documents/workspace/chromedriver"); ChromeOptions option = new ChromeOptions(); option.addArguments("disable-infobars"); List<Object> list = new ArrayList<>(); list.add("enable-automation"); option.setExperimentalOption("excludeSwitches", list); option.addArguments("--disable-blink-features", "--disable-blink-features=AutomationControlled"); BrowserMobProxy proxy = new BrowserMobProxyServer(); proxy.start(0); proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT); proxy.setHarCaptureTypes(CaptureType.RESPONSE_CONTENT); proxy.newHar("qunanr"); Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy); seleniumProxy.setHttpProxy("localhost:" + proxy.getPort()); seleniumProxy.setSslProxy("localhost:" + proxy.getPort()); proxy.addRequestFilter((request, contents, messageInfo) -> { // System.out.println("这里拦截请求" + request.uri() + "" + contents.getTextContents() + ">>>" // + request.headers().get("Cookie")); return null; }); option.setProxy(seleniumProxy); option.setAcceptInsecureCerts(true); option.setExperimentalOption("useAutomationExtension", false); proxy.addResponseFilter((response, contents, messageInfo) -> { if (messageInfo.getUrl().contains("wbdflightlist")) { // 这个接口是机票列表详细信息,可解析后使用 // System.out.println(response.status() + ":" + response.headers().get("cookie") + ">>>" // + contents.getTextContents()); } }); String url = "https://m.flight.qunar.com/ncs/page/flightlist?depCity=%E4%B8%8A%E6%B5%B7&arrCity=%E5%8C%97%E4%BA%AC&goDate=2022-01-01&from=touch_index_search&child=0&baby=0&cabinType=0"; WebDriver webDriver = new ChromeDriver(option); webDriver.get(url); List<WebElement> resultElements = webDriver.findElements(By.className("list-content")).get(0).findElements(By.tagName("li")); for (int i = 0; i < resultElements.size(); i++) { System.out.println((i+1) + "\n" + resultElements.get(i).getText()); System.out.println(); } proxy.stop(); webDriver.quit(); } 
今天的文章 去哪儿网手机版机票数据分享到此就结束了,感谢您的阅读。
编程小号
上一篇 2024-12-10 12:11
下一篇 2024-12-10 12:06

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/82728.html