python用Selenium爬取携程网机票信息

一、问题说明

1、selenium库是爬虫过程中比较讨巧的一个第三方库,它能够跳过js、ajax等交互,上手比较容易。
2、基础代码是根据其他博主参考而来,但携程网站不断变化,除ID等不变的信息外,其余都已发生变化,因此,仔细对比之后,改进并编写了以下代码,发布于2021年10月19日。
3、如果出现报错,请尝试修改下面代码中 time.sleep()函数参数。
4、要爬取自己想要的数据,只需修改出发地和到达地以及出发时间,另外注意修改浏览器驱动,本人用的是MicroSoft Edge,驱动到对应的网站下载,下载后要更名驱动并修改 driver_path参数。
4、目前只是一个基础版本,后续可能会发布更新版,如加入直飞、中转、经停等个性化数据爬取。
5、代码只供学习参考,请勿商用!

二、代码

# -*- coding:utf-8 -*-
# 利用selenium爬取携程
# Author: KingStar
import time
from selenium import webdriver
from bs4 import BeautifulSoup

def page_select_function(driver_path):
    driver = webdriver.Edge(executable_path=driver_path)
    driver.get('https://www.ctrip.com/')
    time.sleep(1)
    # 窗口最大化
    driver.maximize_window()
    # 从首页选择进入机票页面
    input_tag_slect = driver.find_element_by_class_name('s_tab_nocurrent')
    input_tag_slect.click()
    time.sleep(1)
    # 选择日期
    input_tag_time = driver.find_element_by_id('FD_StartDate')
    input_tag_time.send_keys('2021-10-20')
    time.sleep(1)
    # 选择出发城市
    input_tag_depart_city = driver.find_element_by_id('FD_StartCity')
    input_tag_depart_city.send_keys('厦门')
    time.sleep(1)
    # 选择到达城市
    input_tag_arrive_city = driver.find_element_by_id('FD_DestCity')
    input_tag_arrive_city.send_keys('兰州')
    time.sleep(1)
    # 开始搜索
    input_tag_search = driver.find_element_by_id('FD_StartSearch')
    # driver.find_element(By.CSS_SELECTOR, "#submit").send_keys(Keys.ENTER)
    driver.execute_script("arguments[0].click();", input_tag_search)
    # input_tag_search.click()
    time.sleep(3)
    # 关闭紧急公告提示
    tag_close = driver.find_element_by_class_name('close-icon')
    tag_close.click()
    time.sleep(1)
    # 只选择经停
    tag_select = driver.find_element_by_class_name('auto_cursor')
    tag_select.click()
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="domestic_filter_group_trans_and_train__trans_count"]/li[2]/span').click()
    driver.find_element_by_class_name('flight-part').click()
    time.sleep(1)
    '''
    # 只选择直飞
    direct_flight_tag_click = driver.find_element_by_class_name('form-label')
    direct_flight_tag_click.click()
    time.sleep(2)
    '''
    for i in range(1):
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(1)
    return driver

def data_acquistion(driver):
    source = driver.page_source
    bs = BeautifulSoup(source, 'html.parser')
    divs = bs.find_all('div', class_='flight-item domestic')
    return divs

def data_treating(divs):
    for div in divs:
        try:
            airlineName = div.find('div', class_='airline-name').get_text()
            flightNumber = div.find_all('span', class_='plane-No')[0].get_text()
            # craftTypeName = div.find('span', class_='direction_black_border low_text').string
            departureTime = div.find('div', class_='depart-box').find('div', class_='time').string
            arrivalTime = div.find('div', class_='arrive-box').find('div', class_='time').get_text()
            lowestPrice = div.find('span', class_='price').get_text()
            print(airlineName, '\t', flightNumber, '\t', departureTime, '\t', arrivalTime, '\t', lowestPrice)
            time.sleep(1)
        except:
            print(无该信息)

def main():
    driver_path = r'C:\MicrosoftWebDriver.exe'
    driver = page_select_function(driver_path)
    divs = data_acquistion(driver)
    data_treating(divs)
    driver.close()

if __name__ == '__main__':
    main()



  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值