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-文件操作,可写入操作,文件存在就写入,文件不存在先创建






  • 相关阅读:
    bzoj 2337 [HNOI2011]XOR和路径【高斯消元+dp】
    bzoj 3196 Tyvj 1730 二逼平衡树【线段树 套 splay】
    bzoj 3528 [Zjoi2014]星系调查【树链剖分+数学】
    bzoj 2127 happiness【最小割+dinic】
    bzoj 3110 [Zjoi2013]K大数查询【树套树||整体二分】
    bzoj 4137 [FJOI2015]火星商店问题【CDQ分治+可持久化trie】
    运用背景橡皮擦抠透明郁金香
    使用快速通道抠荷花
    抠图总结
    花纹的选区
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/14243778.html
Copyright © 2011-2022 走看看