2025年4月28日 星期一 乙巳(蛇)年 正月廿九 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python爬虫第7章(1)selenium示例(2)

时间:11-05来源:作者:点击数:53

selenium简介

能不能让我的程序连接到浏览器 . 让浏览器来完成各种复杂的操作, 我们只接受最终的结果

selenium: 自动化测试工具

可以: 打开浏览器. 然后像人一样去操作浏览器

程序员可以从selenium中直接提取网页上的各种信息

环境搭建:

​ pip install selenium -i 清华源

​ 下载浏览器驱动:https://npm.taobao.org/mirrors/chromedriver

​ 把解压缩的浏览器驱动 chromedriver 放在python解释器所在的文件夹

看自己上一个博客安装的:https://www.cdsy.xyz/computer/programme/Python/20221105/cd166758753237777.html

selenium的引入

  • from time import sleep
  • # 让selenium启动谷歌浏览器
  • from selenium.webdriver import Chrome
  • #1.创建浏览器对象
  • web=Chrome()
  • #2.打开一个网址
  • 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) # 让浏览器缓一会儿
  • # 找到输入框. 输入python => 输入回车/点击搜索按钮
  • search=web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python",Keys.ENTER) # Keys.ENTER是回车
  • sleep(1)
  • # 查找存放数据的位置. 进行数据提取
  • # 找到页面中存放数据的所有的div
  • div_list = web.find_elements_by_xpath('//*[@id="jobList"]/div[1]/div') #注意elements,要不然element只能获取一个元素
  • 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/')
  • # page_text=bro.page_source
  • # #数据解析
  • # tree=etree.HTML(page_text)
  • #标签定位
  • search_input=bro.find_element_by_id('q')
  • #标签交互
  • search_input.send_keys('Iphone')
  • #执行一组js程序
  • bro.execute_script('window.scrollTo(0,document.body.scrollHeight)') #x向下滚动一平幕
  • sleep(2)
  • #点击搜索按钮
  • btn=bro.find_element_by_css_selector('.btn-search') #这里填写的是class的属性值
  • 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) # 让浏览器缓一会儿
  • # 找到输入框. 输入python => 输入回车/点击搜索按钮
  • search=web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python",Keys.ENTER) # Keys.ENTER是回车
  • sleep(1)
  • # 查找存放数据的位置. 进行数据提取
  • # 找到页面中存放数据的所有的div
  • div_list = web.find_elements_by_xpath('//*[@id="jobList"]/div[1]/div') #注意elements,要不然element只能获取一个元素
  • 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/')
  • # page_text=bro.page_source
  • # #数据解析
  • # tree=etree.HTML(page_text)
  • #标签定位
  • search_input=bro.find_element_by_id('q')
  • #标签交互
  • search_input.send_keys('Iphone')
  • #执行一组js程序
  • bro.execute_script('window.scrollTo(0,document.body.scrollHeight)') #x向下滚动一平幕
  • sleep(2)
  • #点击搜索按钮
  • btn=bro.find_element_by_css_selector('.btn-search') #这里填写的是class的属性值
  • 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/')
  • #点击X
  • web.find_element_by_id('cboxClose').click() # =web.find_element_by_xpath('//*[@id="cboxClose"]').click()
  • sleep(1)
  • #输入后搜索
  • # search=web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python",Keys.ENTER) # Keys.ENTER是回车
  • 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()
  • # 如何进入到进窗口中进行提取
  • # 注意, 在selenium的眼中. 新窗口默认是不切换过来的.
  • web.switch_to.window(web.window_handles[-1]) #,选项卡,打开最后一个窗口,-1是最后一个
  • # 在新窗口中提取内容
  • job_detail=web.find_element_by_xpath('//*[@id="job_detail"]/dd[2]/div').text
  • print(job_detail)
  • sleep(5)
  • # 关掉子窗口
  • web.close() #关闭窗口,但是爬虫视角还在关闭的窗口哪里
  • # 变更selenium的窗口视角. 回到原来的窗口中
  • 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()
  • # 如果页面中遇到了 iframe如何处理
  • web.get("https://www.91mjw.cc/video/76-1-0.html")
  • # 处理iframe的话. 必须先拿到iframe. 然后切换视角到iframe . 再然后才可以拿数据
  • iframe=web.find_element_by_xpath('//*[@id="cciframe"]')
  • # 切换到iframe
  • web.switch_to.frame(iframe)
  • # web.switch_to.default_content() # 切换回原页面
  • 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)): # i就是每一个下拉框选项的索引位置
  • sel.select_by_index(i) # 按照索引进行切换
  • sleep(2)
  • table=web.find_element_by_xpath('//*[@id="TableList"]/table')
  • print(table.text) # 打印所有文本信息
  • print("----------------------------------------------------------------")
  • print('运行完毕')
  • web.close()
  • # 如何拿到页面代码Elements(经过数据加载以及js执行之后的结果的html内容)
  • #web.page_source
  • print(web.page_source)

实现规避检测

  • #实现规避检测
  • from selenium.webdriver import ChromeOptions
  • #实现规避检测
  • option = ChromeOptions()
  • option.add_experimental_option('excludeSwitches', ['enable-automation'])
  • #如何实现让selenium规避被检测到的风险
  • bro=webdriver.Chrome(executable_path='./chromedriver',chrome_options=chrome_options,options=option)

按照索引

在这里插入图片描述
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门