zoukankan      html  css  js  c++  java
  • Python 自动化获取酒店搜索信息

    使用python 自动化获取酒店的价格,评分 和 酒店名字

    使用到了:

    selenium webdriver

    文件写作操作

    遍历

    range(len(list))

    等待时间(我这里等待时间写的不好,代码需要优化的很多,应该用webdriverwait,我是用的是隐式等待和强制等待)

    脚本如下

    #导入需要用到的包,一个是webdriver,UI自动化使用,一个是time,等待时间使用

    from selenium import webdriver
    import time

    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.get("http://www.elong.com/")
    driver.implicitly_wait(7) #隐式等待,我觉得我脚本里的等待时间用的不优化
    driver.find_element_by_xpath('//input[@data-bindid="city"]').click() #定位:相对定位,自己写,不要复制

    driver.find_element_by_xpath('//li[@title="上海"]').click()
    driver.find_element_by_xpath('//input[@data-bindid="checkIn"]').clear()
    driver.find_element_by_xpath('//input[@data-bindid="checkIn"]').send_keys("2021-01-07")
    driver.find_element_by_xpath('//input[@data-bindid="checkOut"]').clear()
    driver.find_element_by_xpath('//input[@data-bindid="checkOut"]').send_keys("2021-01-08")

    driver.find_element_by_xpath('//dt[@class="w60"]').click()


    driver.find_element_by_xpath('//div[@id="domesticDiv"]//dt[text()="目的地"]').click()

    #1.XPath相对定位
    #//标签名[@属性=值]
    #//标签名[text()=值]
    #//标签名[text()=值]//标签名[标签名[text()=值]
    #driver.find_element_by_xpath('//span[@data-bindid="search"]').click()

    time.sleep(2)
    driver.find_element_by_xpath('//span[@data-bindid="search"]').click()


    names=driver.find_elements_by_xpath("//span[@class='info_cn']")
    prices=driver.find_elements_by_xpath("//span[@class='h_pri_num ']")
    previews=driver.find_elements_by_xpath("//span[@class='h_info_comt_bg']")
    fs=open("酒店搜索数据.txt","w",encoding="utf-8")
    for index in range(len(names)):
    #for index in [0,1,2,3,4,5,6,7,8,9,10,11,12]:
    print(names[index].text,prices[index].text,previews[index].text)
    fs.write(names[index].text+" ")
    fs.write(prices[index].text+" ")
    fs.write(previews[index].text+" ")
    fs.close()

    #文件操作酒店搜索数据.txt
    #读写操作,创建一个文件,写入数据,然后关闭
    #open-文件操作,可写入操作,文件存在就写入,文件不存在先创建






  • 相关阅读:
    d2admin框架学习
    手机访问本地配置域名下的项目
    laydate 限制结束日期不能大于起始日期
    学习MVC之租房网站(十一)-定时任务和云存储
    学习MVC之租房网站(十)-预约和跟单
    学习MVC之租房网站(九)-房源显示和搜索
    学习MVC之租房网站(八)- 前台注册和登录
    学习MVC之租房网站(七)-房源管理和配图上传
    《程序员修炼之道》笔记(九)
    《程序员修炼之道》笔记(八)
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/14243778.html
Copyright © 2011-2022 走看看