from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://search.jd.com/Search?keyword=iphone8&enc=utf-8&suggest=4.def.0.V16&wq=iphone&pvid=c7ae27e0d8b64d2fa344c68054e992f8')
time.sleep(3)
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(3)
driver.get
driver.quit()
# 使页面向下滑动 加载全部页面 返回页面html
def scroll(driver):
try:
# 设置浏览器的宽高 防止得到的WebElement的状态is_displayed为False 即不可见
driver.set_window_size(1200, 800)
# 返回滚动高度
last_height = driver.execute_script('return document.dody.scrollHeight')
while True:
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
time.sleep(3)
new_height = driver.execute_script('return document.dody.scrollHeight')
if new_height == last_height:
break
last_height = new_height
except:
print('加载失败')
# 页面完全加载
html = driver.page_source
return html
案例:V电影
import random
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap['phantomjs.page.settings.userAgent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'
driver = webdriver.Chrome(desired_capabilities=dcap)
driver.set_window_size(1200, 800)
driver.get('https://www.vmovier.com/')
time.sleep(2)
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(random.random()*10)
# 计算新的滚动高度并与上一个滚动高度进行比较
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
html = driver.page_source
print(html)
driver.close()