您当前的位置: 首页 > 

lootaa

暂无认证

  • 1浏览

    0关注

    68博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

去哪儿网网页版机票数据

lootaa 发布时间:2021-12-03 22:19:59 ,浏览量:1

目录

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

选型

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

概述

去哪儿网网页版机票查询的网址是https://flight.qunar.com/ 本篇使用成都到北京的航线进行测试。

Java开发 引入包
        
		  org.seleniumhq.selenium
		  selenium-java
		
	    
		    net.lightbody.bmp
		    browsermob-core
		    2.1.5
		
		
		    com.google.guava
		    guava
		     22.0
		
		
			net.lightbody.bmp
			browsermob-legacy
			2.1.5
		

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

请求配置

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

		System.setProperty("webdriver.chrome.driver",  "chromedriver的完整路径名称");
		
		ChromeOptions option = new ChromeOptions();
        option.addArguments("disable-infobars");
        List 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://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport=%E6%88%90%E9%83%BD&searchArrivalAirport=%E5%8C%97%E4%BA%AC&searchDepartureTime=2022-01-01&nextNDays=0&startSearch=true&fromCode=CTU&toCode=BJS&from=qunarindex&lowestPrice=null";
		WebDriver webDriver = new ChromeDriver(option);
		webDriver.get(url);
		List resultElements = webDriver.findElements(By.className("b-airfly"));
	    System.out.println(resultElements.size());
	    if (!resultElements.isEmpty()) {
	    	for(int i = 0; i < resultElements.size(); i++) {
	    		List 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 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://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport=%E6%88%90%E9%83%BD&searchArrivalAirport=%E5%8C%97%E4%BA%AC&searchDepartureTime=2022-01-01&nextNDays=0&startSearch=true&fromCode=CTU&toCode=BJS&from=qunarindex&lowestPrice=null";
		WebDriver webDriver = new ChromeDriver(option);
		webDriver.get(url);
		List resultElements = webDriver.findElements(By.className("b-airfly"));
	    System.out.println(resultElements.size());
	    if (!resultElements.isEmpty()) {
	    	for(int i = 0; i < resultElements.size(); i++) {
	    		List 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();

	}
关注
打赏
1663829960
查看更多评论
立即登录/注册

微信扫码登录

0.0346s