您当前的位置: 首页 > 

lootaa

暂无认证

  • 0浏览

    0关注

    68博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

去哪儿网手机版机票数据

lootaa 发布时间:2021-12-01 01:51:45 ,浏览量:0

目录

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

选型

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

概述

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

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

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

微信扫码登录

0.0412s