# 获取当前页面的高度
last_height = driver.execute_script("return document.body.scrollHeight")
# 模拟下拉操作,直到滑动到底部
while True:
# 模拟下拉操作
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# 等待页面加载
time.sleep(2)
# 获取当前页面的高度
new_height = driver.execute_script("return document.body.scrollHeight")
# 判断是否已经到达页面底部
if new_height == last_height:
break
# 继续下拉操作
last_height = new_height
time.sleep(2)
# 下拉完毕后返回顶部
driver.execute_script("var q=document.documentElement.scrollTop=0")
time.sleep(2)
# # 解析网页
page = driver.page_source
soup = BeautifulSoup(page,'html.parser')
pages = soup.find_all(class_='ps-wrapper bg-white px-16 py-20 mb-16')
#获取共有多少条数据
print(len(pages))
fori in range(1,len(pages)):
jj_name=driver.find_element(By.XPATH,'//*[@id="__nuxt"]/aside/div/div[2]/div[1]/div[5]/div[' + str(i) + ']/div[1]/a').text
然后把len(pages)当做变量传入xpath中
需要注意的地方是,下拉到底部之后需要在上划到顶部才能不报错然后输出len(pages)
其中下拉代码参考
https://blog.youkuaiyun.com/naer_chongya/article/details/131071222