python爬虫 使用selenium+phontomjs 模拟点击输入 获取东航加载后的源码 机票价格
2017-12-06 13:42
2106 查看
#coding:utf8
from selenium import webdriver import time driver = webdriver.PhantomJS() driver.get('http://www.ceair.com/flight2014/pvg-nay-171201_CNY.html') time.sleep(1) driver.save_screenshot('5.png') # 定位到出发城市框 ele_fromcity = driver.find_element_by_xpath("/html/body/section[@id='main-area']/section[@class='flight-booking']/section[@class='booking-search clearfix']/div[@id='trip']/dl/dd[1]/span[@id='citySelect0']/input[@class='input city citySelect-text']") # 清除框内的默认城市 ele_fromcity.clear() # 输入你想要的出发城市 ele_fromcity.send_keys(u'杭州') time.sleep(1) # 因为是无界面浏览器,每一步操作完成后,可以截图查看 driver.save_screenshot('6.png') # 买过机票的一般都知道,填写出发城市时,会有下拉框弹出显示机场名,需要点击二次确认 # 定位到下拉框 ele_fromcity_list = driver.find_element_by_xpath('/html/body/div/ul/li') # 点击确定 ele_fromcity_list.click() # 定位到目的城市框 ele_tocity = driver.find_element_by_xpath('//*[@id="citySelect1"]/input[1]') # 清除默认城市 ele_tocity.clear() # 输入你想要到达的城市 ele_tocity.send_keys(u'北京') time.sleep(1) driver.save_screenshot('7.png') # 定位到下拉框 ele_tocity_list = driver.find_element_by_xpath('/html/body/div/ul/li') # 点击确认 ele_tocity_list.click() # 定位到出发日期框 ele_fromdate = driver.find_element_by_xpath('//*[@id="trip"]/dl/dd[4]/input') # 清除默认日期 ele_fromdate.clear() # 输入指定的日期 ele_fromdate.send_keys('2017-12-10') driver.save_screenshot('8.png') # 全部输入完成后 需要点击搜索,然后显示出相应的机票价格和班次 # 定位到搜索框 ele_search = driver.find_element_by_xpath('//*[@id="btn_flight_search"]') # 点击搜索 ele_search.click() # 等待页面加载3秒 time.sleep(3) driver.save_screenshot('9.png') # 获取加载后的页面源码 content=driver.page_source.encode('utf-8') print(content)
通过无界面浏览器,获得网页源码,能很好的处理大部分动态加载的网页
提取机票价格的内容比较简单,就不贴出来了
附上几张截图,便于理解
5.png
6.png
7.png
8.png
9.png
相关文章推荐
- 浅谈python爬虫使用Selenium模拟浏览器行为
- python爬虫项目- 爬取厦门航空官网机票价格 selenium 多进程爬取
- python基于selenium+PhantomJS实现用提交表单,并点击按钮功能,获取返回的页面源码和ur等信息l。
- python爬虫:使用Selenium模拟浏览器行为
- [Java爬虫] 使用 HtmlUnit + Xpath 模拟点击、动态获取信息
- 使用python+selenium制作浏览器爬虫,彻底解决ajax异步加载问题(待更新)
- [Python爬虫]利用Selenium等待Ajax加载及模拟自动翻页,爬取东方财富网公司公告
- Python爬虫学习——使用selenium和phantomjs爬取js动态加载的网页
- python 针对selenium+phontomjs等模拟浏览器爬虫的反爬技术点
- 使用firefox和selenium模拟点击js获取更多评论
- 使用python爬虫获取黄金价格的核心代码
- Python爬虫(二十三)_selenium案例:动态模拟页面点击
- Python爬虫——5-2.使用selenium和phantomjs模拟QQ空间登录
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
- python-19:使用正则表达式从源码中获取想要的内容
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
- Python爬虫实战(5):模拟登录淘宝并获取所有订单
- 爬虫之自动保存文档-使用python/selenium
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒