selenium简介
能不能让我的程序连接到浏览器 . 让浏览器来完成各种复杂的操作, 我们只接受最终的结果
selenium: 自动化测试工具
可以: 打开浏览器. 然后像人一样去操作浏览器
程序员可以从selenium中直接提取网页上的各种信息
环境搭建:
? pip install selenium -i 清华源
? 下载浏览器驱动:https://npm.taobao.org/mirrors/chromedriver
? 把解压缩的浏览器驱动 chromedriver 放在python解释器所在的文件夹
看自己上一个博客安装的:https://blog.csdn.net/weixin_44953928/article/details/121444859
selenium的引入
from time import sleep
from selenium.webdriver import Chrome
web=Chrome()
web.get("https://www.baidu.com/")
print(web.title)
sleep(5)
web.quit()
selenium的简单操作(1)
from time import sleep
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
web=Chrome()
web.get('https://lagou.com/')
el=web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a')
el.click()
sleep(1)
search=web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python",Keys.ENTER)
sleep(1)
div_list = web.find_elements_by_xpath('//*[@id="jobList"]/div[1]/div')
for div in div_list:
name=div.find_element_by_xpath('./div[1]/div[1]/div[1]/a').text
job_price=div.find_element_by_xpath('./div[1]/div[1]/div[2]/span').text
print(name,job_price)
sleep(1)
web.quit()
from selenium import webdriver
from lxml import etree
from time import sleep
bro=webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://www.taobao.com/')
search_input=bro.find_element_by_id('q')
search_input.send_keys('Iphone')
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)
btn=bro.find_element_by_css_selector('.btn-search')
btn.click()
selenium的简单操作(1)
from time import sleep
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
web=Chrome()
web.get('https://lagou.com/')
el=web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a')
el.click()
sleep(1)
search=web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python",Keys.ENTER)
sleep(1)
div_list = web.find_elements_by_xpath('//*[@id="jobList"]/div[1]/div')
for div in div_list:
name=div.find_element_by_xpath('./div[1]/div[1]/div[1]/a').text
job_price=div.find_element_by_xpath('./div[1]/div[1]/div[2]/span').text
print(name,job_price)
sleep(1)
web.quit()
from selenium import webdriver
from lxml import etree
from time import sleep
bro=webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://www.taobao.com/')
search_input=bro.find_element_by_id('q')
search_input.send_keys('Iphone')
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)
btn=bro.find_element_by_css_selector('.btn-search')
btn.click()
窗口间的切换
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from time import sleep
web = Chrome()
web.get('https://lagou.com/')
web.find_element_by_id('cboxClose').click()
sleep(1)
web.find_element_by_id('search_input').send_keys('python',Keys.ENTER)
sleep(1)
web.find_element_by_xpath('//*[@id="jobList"]/div[1]/div[1]/div[1]/div[1]/div[1]/a').click()
web.switch_to.window(web.window_handles[-1])
job_detail=web.find_element_by_xpath('//*[@id="job_detail"]/dd[2]/div').text
print(job_detail)
sleep(5)
web.close()
web.switch_to.window(web.window_handles[0])
iframe如何处理
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
web = Chrome()
web.get("https://www.91mjw.cc/video/76-1-0.html")
iframe=web.find_element_by_xpath('//*[@id="cciframe"]')
web.switch_to.frame(iframe)
tx = web.find_element_by_xpath('').text
print(tx)
无头浏览器
from selenium.webdriver import Chrome
from selenium.webdriver.support.select import Select
from time import sleep
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disbale-gpu")
web=Chrome(options=opt)
web.get("https://www.endata.com.cn/BoxOffice/BO/Year/index.html")
sleep(2)
sel_el=web.find_element_by_xpath('//*[@id="OptionDate"]')
sel=Select(sel_el)
for i in range(len(sel.options)):
sel.select_by_index(i)
sleep(2)
table=web.find_element_by_xpath('//*[@id="TableList"]/table')
print(table.text)
print("----------------------------------------------------------------")
print('运行完毕')
web.close()
print(web.page_source)
实现规避检测
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
bro=webdriver.Chrome(executable_path='./chromedriver',chrome_options=chrome_options,options=option)
————————————————
版权声明:本文为CSDN博主「weixin_44953928」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44953928/article/details/121454826
按照索引
|