用selenium做翻页处理
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.maximize_window() #可以转换成大屏
driver.get('https://bj.fang.lianjia.com/loupan/') #网址
# while True:
# driver.execute_script('window.scrollBy(0,document.body.scrollHeight)')#获取当前屏幕的高度
def scroll():
while True:
check_height = driver.execute_script('return document.body.scrollHeight;')
for i in range(2):
time.sleep(0.5)
driver.execute_script('window.scrollBy(0,1500)')
check_height1 = driver.execute_script('return document.body.scrollHeight;')
if check_height == check_height1:
break
for i in range(5):
#当前页面滑倒底部
scroll()
#点击下一页
driver.find_element_by_xpath('//a[@class="next"]').click()
time.sleep(1)
|