以下圖是咱們要爬取的數據python
找到每一個更多,而後點開。web
# 點擊更多,將未顯示的地址暴露出來 more_click = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'fn-more'))) for a in more_click: a.click()
當全部的更多都被點開後,光標已經挪動到了最底層的位置,這時須要咱們將光標再返回到頂部。
點擊返回頂部的按鈕。ide
#點擊回到頂部,從頭開始爬取數據 to_top = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'toTop'))) to_top.click()
而後再開始一級一級的爬取數據。性能
完整版代碼ui
考慮到性能,因此是使用了協程gevent模塊。spa
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from selenium.webdriver.support.ui import WebDriverWait 4 from selenium.webdriver.support import expected_conditions as EC 5 import gevent 6 7 8 browser = webdriver.Ie('E:\python_server\IEDriverServer') 9 wait = WebDriverWait(browser, 10) 10 11 browser.get("https://www.dianping.com/citylist/citylist?citypage=1") 12 13 def main(): 14 15 # 點擊更多,將未顯示的地址暴露出來 16 more_click = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'fn-more'))) 17 for a in more_click: 18 a.click() 19 20 #點擊回到頂部,從頭開始爬取數據 21 to_top = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'toTop'))) 22 to_top.click() 23 24 #地區 25 adress = browser.find_elements_by_class_name('vocabulary') 26 for i in adress: 27 print(i.text) 28 29 #直轄市,港澳臺 30 adress_city_name_1 = browser.find_elements_by_class_name('terms') 31 for i in adress_city_name_1: 32 print(i.text) 33 34 #地級市,縣級市 35 adress_city_name_2 = browser.find_elements_by_class_name('terms-open') 36 for i in adress_city_name_2: 37 print(i.text) 38 39 browser.close() 40 41 if __name__ == '__main__': 42 gevent.joinall([ 43 gevent.spawn(main), 44 ])
結果部分截圖code