# -*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.common.keys import Keys import time from selenium.webdriver.firefox.options import Options options = Options() options.headless=True driver = webdriver.Firefox(executable_path ="/home/custdev/test_core/firefox/geckodriver",options=options) driver.get('http://api.map.baidu.com/lbsapi/getpoint/index.html') time.sleep(5) element1 = driver.find_element_by_link_text("更换城市") element1.click() element2 = driver.find_element_by_link_text("上海") element2.click() time.sleep(5) element3 = driver.find_element_by_id("localvalue") element3.send_keys("酒店") element3.send_keys(Keys.RETURN) #此步为关键格式!这样html内容才会改变 time.sleep(2) element = '' i=0 while True: i+=1 print('第'+str(i)+'页') element += driver.find_element_by_class_name('local_s').text #print(driver.page_source) if '下一页' not in driver.page_source: break loc = driver.find_element_by_link_text("下一页") loc.click() time.sleep(5) file_handle = open("m.txt",mode = 'w+') file_handle.write(element) file_handle.close() driver.quit()