selenium模块的基本使用
问题:selenium模块和爬虫之间具体怎样的联系?
答:
-
便捷获取网站中动态加载的数据
-
便捷实现模拟登录
问题:什么是selenium模块?
答:
-
基于浏览器自动化的一个模块
selenium使用流程:
-
环境安装:pip install selenium
-
下载一个浏览器的驱动程序(谷歌浏览器)
-
下载路径:http://chromedriver.storage.googleapis.com/index.html
-
驱动程序和浏览器的映射关系:selenium之 chromedriver与chrome版本映射表(更新至v2.46)_灰蓝-CSDN博客_chromedriver
-
-
实例化一个浏览器对象:
from selenium import webdriver from lxml import etree from time import sleep bro = webdriver.Chrome(executable_path='chromedriver') #实例化一个浏览器对象(传入浏览器驱动器) bro.get('http://scxk.nmpa.gov.cn:81/xk/') #浏览器发起一个get请求 page_text = bro.page_source #page_source获取浏览器当前页面的页面源码数据 #获取企业名称 tree = etree.HTML(page_text) li_list = tree.xpath('.//ul[@id="gzlist"]/li') for li in li_list: name = li.xpath('./dl/@title')[0] print(name) sleep(5) bro.quit() #关闭浏览器4.
-
编写基于浏览器自动化的操作代码
-
发起请求:get(url)
-
标签定位:find系列的方法
-
标签交互:send_key('xxx')
-
执行js程序:excut_script('jsCode')
-
前进、后退:back()、forward()
-
关闭浏览器:quit()
-
-
selenium处理iframe
-
如果定位的标签存在于selenium标签中,则必须使用switch_to.frame(id)
-
动作链(拖动):
-
from selenium.webdriver import ActionChains
-
实例化一个动作链对象:action = ActionChains(bro)
-
clink_and_hold(div):点击且长按操作
-
move_by_offset(x,y)
-
perform():让动作链立即执行
-
action.release():释放动作链对象
-
-
示例(注释的都是精华)
示例一:淘宝-搜索-滚屏
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path='chromedriver')
bro.get('https://www.taobao.com/')
search_input = bro.find_element('id', 'q')
#标签定位
search_input.send_keys('Iphone')
#标签交互
sleep(2)
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
#执行一组js程序
#该程序需要在console中写一串代码:window.scrollTo(0,document.body.scrollHeight)
sleep(2)
btn = bro.find_elements('xpath', '//*[@id="J_TSearchForm"]/div[1]/button')[0]
btn.click()
#点击搜索按钮
bro.get('https://www.baidu.com')
sleep(2)
bro.back()
#回退
sleep(2)
bro.forward()
#前进
sleep(2)
bro.quit()
示例二:动作链(拖动)-frame
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
bro = webdriver.Chrome(executable_path='chromedriver')
bro.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
bro.switch_to.frame('iframeResult')#括号内是frame的id
#如果定位的标签位于frame标签中,则必须通过上述操作进行标签定位
div = bro.find_element('id', 'draggable')
action = ActionChains(bro)
#动作链
action.click_and_hold(div)
#点击长按指定标签
for i in range(5):
action.move_by_offset(17, 0).perform()
#perform()立即执行动作链操作
#move_by_offset(x, y)x:水平方向移动距离,y:竖直方向移动距离
sleep(1)
action.release()
#释放动作链
bro.quit()
示例三:模拟登录QQ空间
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome('chromedriver')
bro.get('https://qzone.qq.com/')
bro.switch_to.frame('login_frame')
a_tag = bro.find_element('id', 'switcher_plogin')
a_tag.click()
userName_tag = bro.find_element('id', 'u')
passWord_tag = bro.find_element('id', 'p')
sleep(2)
userName_tag.send_keys('2805487978')
sleep(1)
passWord_tag.send_keys('******')
sleep(1)
btn = bro.find_element('id', 'login_button')
btn.click()
sleep(2)
bro.quit()
示例四:无头浏览器+反检测
from selenium import webdriver
from time import sleep
#实现规避检测/实现无可视化页面
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument('headless')
#添加:options=options
bro = webdriver.Chrome(executable_path='chromedriver', options=options)
bro.get('https://www.baidu.com')
bro.quit()
示例五:古诗文网模拟登录
流程:
-
使用selenium打开登录页面
-
对当前selenium打开的这张页面进行截图
-
对当前图片的局部区域(验证码图片)进行裁剪
-
好处:将验证码图片和模拟登录一一对应
-
-
使用超级鹰识别验证码图片(坐标)
from selenium import webdriver
from time import sleep
from PIL import Image
from 超级鹰 import Chaojiying_Client
bro = webdriver.Chrome(executable_path='chromedriver')
bro.get('https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx')
sleep(1)
bro.maximize_window()
#浏览器最大化显示
bro.save_screenshot('aa.png')
#save_screenshot()将当前整张页面截取并保存
#确定验证码图片对应的左上角和右下角的坐标(裁剪的区域就确定了)
code_img_ele = bro.find_element('xpath', '//*[@id="imgCode"]')
location = code_img_ele.location
#验证码图片左上角的坐标:x,y
size = code_img_ele.size
#验证码图片对应的宽和高
rangle = (
location['x']*1.25, location['y']*1.25, (location['x']+size['width'])*1.25, (location['y']+size['height'])*1.25
)
i = Image.open('./aa.png')
frame = i.crop(rangle)
frame.save('./a.png')
chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '925926') #用户中心>>软件ID 生成一个替换 96001
im = open('a.png', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 1902)['pic_str'])
yan = chaojiying.PostPic(im, 1902)['pic_str']
bro.find_element('id', 'email').send_keys('账号')
bro.find_element('id', 'pwd').send_keys('密码')
bro.find_element('id', 'code').send_keys(yan)
bro.find_element('id', 'denglu').click()
sleep(2)
bro.quit()








