博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python爬虫之Selenium
阅读量:4680 次
发布时间:2019-06-09

本文共 2021 字,大约阅读时间需要 6 分钟。

首先解决python中使用selenium调用Firefox缺少geckodriver的问题

geckodriver.exe下载地址 : https://pan.baidu.com/s/10Vy9WH1ZpkvdFmZ3T7aw_w ,

解压完成,然后放到python的安装目录与python.exe在同一目录下

Selenium是一个第三方模块,可以完全模拟用户在浏览器上操作(在浏览器上点点点)。 安装
pip3 install selenium
优缺点:        优:无需再自己操作cookie和header        缺:慢 依赖驱动:       Firefox          https://github.com/mozilla/geckodriver/releases       Chrome          http://chromedriver.storage.googleapis.com/index.html 具体使用
#!/usr/bin/env python# coding:utf-8import  selenium.webdriverimport  timedriver = selenium.webdriver.Firefox()url = "http://www.baidu.com"driver.get(url)driver.find_element_by_class_name("s_ipt").send_keys("你好")driver.find_element_by_id("su").click()
 

 selenium的具体使用

#!/usr/bin/env python# -*- coding:utf-8 -*-"""Selenium是一个第三方模块,可以完全模拟用户在浏览器上操作(在浏览器上点点点)。    安装:        pip3 install selenium    优缺点:        优:无需再自己操作cookie和header        缺:慢    依赖驱动:        Firefox            https://github.com/mozilla/geckodriver/releases        Chrome            http://chromedriver.storage.googleapis.com/index.html"""from selenium import webdriver# 配置驱动option = webdriver.ChromeOptions()driver = webdriver.Chrome('/Users/wupeiqi/drivers/chromedriver', chrome_options=option)# 1. 控制浏览器打开指定页面driver.get("https://dig.chouti.com/all/hot/recent/1")# 2. 找到登录按钮btn_login = driver.find_element_by_xpath('//*[@id="login-link-a"]')# 3. 点击按钮btn_login.click()# 4. 找到手机标签input_user = driver.find_element_by_xpath('//*[@id="mobile"]')# 5. 找到密码标签input_pwd = driver.find_element_by_xpath('//*[@id="mbpwd"]')# 6. 输入用户名input_user.send_keys('13121758648')# 7. 输入密码input_pwd.send_keys('woshiniba')# 8. 点击登录按钮input_submit = driver.find_element_by_xpath(    '//*[@id="footer-band"]/div[5]/div/div/div[1]/div[2]/div[4]/div[2]/div/span[1]')input_submit.click()print(driver.get_cookies())# 9. 点击跳转news = driver.find_element_by_xpath('//*[@id="newsContent20646261"]/div[1]/a[1]')# news.click()driver.execute_script("arguments[0].click();", news)# 10.管理浏览器driver.close()
具体使用

 

 

 

 

转载于:https://www.cnblogs.com/weidaijie/p/10447733.html

你可能感兴趣的文章