Python Crawler1:Selenium自动化安装

Python Crawler1:Selenium自动化安装

Selenium库安装

  • Python的Web浏览器交互
pip install selenium

使用Chmore

1. 查看Chmore版本

notion image

2. 下载ChmoreDriver

  • 我这里是最新的,直接在官网首页就可以看到,其他版本可以查看历史版本
notion image
notion image

3. 配置文件

下载后解压到:python文件夹目录下,驱动安装就ok了。
notion image

4. 测试

from selenium import webdriver driver= webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(3) #等待3秒 driver.get("https://baidu.com") driver.quit()
 

5. 其他例子

''' Description: henggao_note version: v1.0.0 Date: 2022-04-02 16:06:01 LastEditors: henggao LastEditTime: 2022-04-03 11:05:53 ''' from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait #初始化一个浏览器(如:谷歌,使用Chrome需安装chromedriver) driver = webdriver.Chrome() #driver = webdriver.PhantomJS() #无界面浏览器 try: #请求网页 driver.get("https://www.baidu.com") #查找id值为kw的节点对象(搜索输入框) input = driver.find_element_by_id("kw") #模拟键盘输入字串内容 input.send_keys("python") #模拟键盘点击回车键 input.send_keys(Keys.ENTER) #显式等待,最长10秒 wait = WebDriverWait(driver,10) #等待条件:10秒内必须有个id属性值为content_left的节点加载出来,否则抛异常。 wait.until(EC.presence_of_element_located((By.ID,'content_left'))) # 输出响应信息 print(driver.current_url) print(driver.get_cookies()) print(driver.page_source) finally: #关闭浏览器 #driver.close() pass
 
 
''' Description: henggao_note version: v1.0.0 Date: 2022-04-04 15:32:27 LastEditors: henggao LastEditTime: 2022-04-06 21:13:21 ''' from selenium import webdriver from selenium.webdriver.common.keys import Keys from time import sleep from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("http://www.baidu.com/") driver.find_element(by=By.CSS_SELECTOR, value="#kw").send_keys("Python") sleep(4) #键盘全选操作 # driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'a') driver.find_element(by=By.CSS_SELECTOR, value="#kw").send_keys(Keys.CONTROL,'a') sleep(2) #键盘复制操作 driver.find_element(by=By.CSS_SELECTOR, value="#kw").send_keys(Keys.CONTROL,'c') sleep(2) #键盘剪切操作 # driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'x') # sleep(2) driver.get("https://cn.bing.com/") sleep(4) driver.find_element(by=By.CSS_SELECTOR, value="#sb_form_q").send_keys(Keys.CONTROL,'v') sleep(4) driver.find_element(by=By.CSS_SELECTOR, value="#search_icon").click() sleep(4) driver.quit()