1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 去哪儿网网页版机票数据

去哪儿网网页版机票数据

时间:2024-03-06 11:12:27

相关推荐

去哪儿网网页版机票数据

目录

携程手机版国内机票数据

携程手机版国际机票数据

携程网页版国内机票数据

携程网页版国际机票数据

去哪儿网手机版机票数据

去哪儿网网页版机票数据

携程手机版机票数据添加代理

去哪儿网网页版机票数据添加代理

选型

请求参数中,有pre、_m_等,需要进行js破解,难度较大,尝试了两次失败了,换方式。使用selenium抓取网页数据。

首选需要下载chromedriver,根据chrome的版本进行选择。链接地址:http://chromedriver./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 = "/site/oneway_list.htm?searchDepartureAirport=%E6%88%90%E9%83%BD&searchArrivalAirport=%E5%8C%97%E4%BA%AC&searchDepartureTime=-01-01&nextNDays=0&startSearch=true&fromCode=CTU&toCode=BJS&from=qunarindex&lowestPrice=null";WebDriver webDriver = new ChromeDriver(option);webDriver.get(url);List<WebElement> resultElements = webDriver.findElements(By.className("b-airfly"));System.out.println(resultElements.size());if (!resultElements.isEmpty()) {for(int i = 0; i < resultElements.size(); i++) {List<WebElement> nums = resultElements.get(i).findElements(By.className("num"));for(int j = 0; j < nums.size(); j++) {String text = nums.get(j).findElement(By.className("n")).getText();System.out.println((i+1) + "\t" + text);}}}proxy.stop();webDriver.quit();

其他

网页端的如果数据过多,会有分页显示,可以模拟对应操作。

// 点击按钮方式(非本网页数据,仅供方式参考)

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 = "/site/oneway_list.htm?searchDepartureAirport=%E6%88%90%E9%83%BD&searchArrivalAirport=%E5%8C%97%E4%BA%AC&searchDepartureTime=-01-01&nextNDays=0&startSearch=true&fromCode=CTU&toCode=BJS&from=qunarindex&lowestPrice=null";WebDriver webDriver = new ChromeDriver(option);webDriver.get(url);List<WebElement> resultElements = webDriver.findElements(By.className("b-airfly"));System.out.println(resultElements.size());if (!resultElements.isEmpty()) {for(int i = 0; i < resultElements.size(); i++) {List<WebElement> nums = resultElements.get(i).findElements(By.className("num"));for(int j = 0; j < nums.size(); j++) {String text = nums.get(j).findElement(By.className("n")).getText();System.out.println((i+1) + "\t" + text);}}}proxy.stop();webDriver.quit();}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。