selenium模拟用户操作浏览器

selenium,有一个强大的python库。能做什么呢?

一般等网页版qq邮箱,首先是打开登录界面,然后输入账号密码,点击登录,就完成了登录。

而用selenium,则可以完成这些操作,就像是人在操作一样。

先安装pip install selenium

然后安装浏览器驱动https://localprod.pandateacher.com/python-manuscript/crawler-html/chromedriver/ChromeDriver.html

selenium模拟用户操作浏览器

这个就是一般操作,对于元素的提取

selenium模拟用户操作浏览器

上面是提取单个元素

下面是提取多个元素

selenium模拟用户操作浏览器

还有就是,这样操作,浏览器会真的打开,又关闭。

有一种静默模式,浏览器不会打开,默默的完成这些操作。

from selenium.webdriver.chrome.options import Options

chrome_options = Options()

chrome_options.add_argument('--headless')

driver = webdriver.Chrome(options = chrome_options)#将浏览器设置为静默模式

 

selenium还能和BeautifulSoup搭配使用

response.text = driver.page_source  #获取网页源码

然后就是熟悉的BeautifulSoup操作了