去哪儿网网页版机票数据

本文介绍了如何利用Java的Selenium库和BrowserMobProxy代理来抓取并解析去哪儿网网页版机票查询数据。通过设置Chrome浏览器选项、添加代理和响应过滤器,成功绕过反爬机制,获取到机票列表和详细信息。同时,展示了如何处理分页和模拟用户交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

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

选型

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

概述

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

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://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<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 = "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<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();

	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lootaa

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值