python 爬虫 通过selenium实现网页拖拽

通过selenium爬取动态网页的过程难免会遇到爬取的内容在下面加载不出来,这时需要通过拖拽网页使网页加载出来再进行内容的爬取。

代码如下:

#导入打开网页需要的库
from selenium import webdriver
import time


#使用火狐浏览器打开网站
browser = webdriver.Firefox()
url = 'http://jd.com'
browser.get(url)


#最大化窗口
browser.maximize_window()


#拖拽网页
#语法:browser.execute_scrips('window.scrollBy(0,高度)','')
#for循环能实现拖拽多次
for i in range(5):
    time.sleep(3)
    browser.execute_script('window.scrollBy(0,600)','')
    
#关闭浏览器
browser.close()

猜你喜欢

转载自blog.csdn.net/qq_40243365/article/details/83017691
今日推荐