0


python 爬虫 入门 六、Selenium

  1. Selenium本来是一个自动测试工具,用于模拟用户对网站进行操作。在爬虫领域也有其用处。

一、下载安装Selenium及附属插件

  1. pip install Selenium
  1. 安装完成后还需要安装一个浏览器驱动,来让python能启动浏览器。
  2. 如果是Edge或者其他基于Chromium的浏览器(如下面的百分浏览器),我们先查看Chromium版本号:

  1. 这里以Edge为例,版本为126.0.2592.68,进入下面的网址,咱们就选最后一个,win版本,解压之后的exe文件就是我们需要的东西,你可以把它放在python解释器目录,项目目录或者其他你找得到的地方。

chromedriver.storage.googleapis.com/index.html

二、selenium的使用

(一)、第一个程序

先来试试第一个程序,它会使用edge打开百度(第一打开时间可能有点长(10s?),并且打开后不久就会自动关闭)然后输出抬头的数据:

  1. import time
  2. from selenium.webdriver import Edge # Edge 可以换成 Chrome/Firefox(火狐)/Ie/BlackBerry······
  3. url = 'http://www.baidu.com'
  4. web = Edge()
  5. web.get(url)
  6. print(web.title)
  7. time.sleep(50)

(二)、以站酷为例

  1. 接下来,我们尝试模拟一下从站酷ZCOOL-设计师互动平台-打开站酷,发现更好的设计!中搜索"网站设计",并打开第一个和第二个文章的全过程
  2. selenium中,我们所有操作,看到的都是已经经过js处理过的页面,也就是说,他是所见即所得。以站酷为例,站酷首页的文章都是二次请求得到的,源代码中没有,用以下代码就能清楚看到。会输出True False,如果不是的话,尝试更改文章名或者延长time.sleep时间,以保证网站完全加载。
  1. import time
  2. import requests
  3. from selenium.webdriver import Edge # Edge 可以换成 Chrome/Firefox(火狐)/Ie/BlackBerry······
  4. headers = {
  5. # 用户代理,某些网站验证用户代理,微微改一下,如果提示要验证码之类的,使用它
  6. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome"
  7. "/118.0.0.0 Safari/537.36",
  8. }
  9. url = 'https://www.zcool.com.cn/'
  10. web = Edge()
  11. web.get(url)
  12. # print(web.page_source)
  13. time.sleep(8)
  14. print('字体合集' in web.page_source) # 字体合集是一个文章名
  15. with requests.get(url=url, headers=headers) as resp:
  16. resp.encoding = "utf-8" # 当页面乱码改这里
  17. # print(resp.text)
  18. print('字体合集' in resp.text)# 字体合集是一个文章名

需要模拟的行为流程

模拟代码

通过以下代码即可获取所需内容:通常来说,人怎么想,就怎么用selenium访问页面。

  1. import time
  2. from selenium.webdriver import Edge # Edge 可以换成 Chrome/Firefox(火狐)/Ie/BlackBerry······
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. url = 'https://www.zcool.com.cn/'
  6. web = Edge()
  7. web.get(url)
  8. time.sleep(3) # 等几秒使得网站完全加载
  9. # 已经进入网站,找到搜索框,输入数据并回车搜索。
  10. # By有By.ID、By.NAME、By.XPATH、By.CSS_SELECTOR等
  11. search_box = web.find_element(By.XPATH, '//*[@id="headerSearchInput"]') # 直接通过检查元素中的xpath获得位置
  12. search_box.send_keys("网站设计")
  13. # 方法一、点击搜索,
  14. # search = web.find_element(By.CLASS_NAME, '_search-icon_1wwm7_457')
  15. # search.click()
  16. # 方法二,按下回车,也可以直接放一块:search_box.send_keys("网站设计",Keys.ENTER)
  17. search_box.send_keys(Keys.ENTER)
  18. time.sleep(3)
  19. # 打开两个文章
  20. img = web.find_element(By.XPATH, '//*[@id="__next"]/main/div/div/div[2]/section[2]/section/section/div[1]/div[1]')
  21. img.click()
  22. img = web.find_element(By.XPATH, '//*[@id="__next"]/main/div/div/div[2]/section[2]/section/section/div[2]/div[1]')
  23. img.click()
  24. time.sleep(2)
  25. # 遇到不能按F12打开控制台和没有右键菜单的情况,应该是拦截了快捷键,点击地址栏然后按F12即可
  26. # 切换窗口获得所需内容
  27. web.switch_to.window(web.window_handles[1])
  28. text = web.find_element(By.XPATH, '//*[@id="__next"]/main/div/section/div[1]')
  29. print(text.text)
  30. web.switch_to.window(web.window_handles[2])
  31. text = web.find_element(By.XPATH, '//*[@id="__next"]/main/div/section/div[1]')
  32. print(text.text)

(三)、其他代码

  1. selenium不太适合做能长久使用的脚本,但短时间内用一两次还是可以的,下面是一些以后可能用到的函数示例
  1. from selenium.webdriver import Edge # Edge 可以换成 Chrome/Firefox(火狐)/Ie/BlackBerry······
  2. from selenium.webdriver.common.action_chains import ActionChains
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.edge.options import Options
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. opt = Options()
  8. opt.add_argument("--disable-blink-features=AutomationControlled") # 防止服务器知道是selenium打开的。
  9. url = 'https://www.zhihu.com/'
  10. web = Edge(options=opt)
  11. web.get(url)
  12. web.implicitly_wait(10) # 隐式等待,接下来所有的查找都至少等10s,如果10s内查到了就继续,否则报错
  13. el = WebDriverWait(web, 10, 0.5).until( # 显示等待,浏览器等待10s每0.5s轮询一次,搜索,如果检测到返回元素
  14. EC.presence_of_element_located((By.NAME, '所需的name'))
  15. )
  16. ActionChains(web).move_to_element_with_offset(el, xoffset=255, yoffset=30) # 模拟点击el元素向右255像素向下30像素的位置

总结:

  1. selenium优点自然是使用比较简单,怎么访问网站就怎么写代码即可,但是访问速度比较慢,需要等待页面JS加载。
标签: python 爬虫 selenium

本文转载自: https://blog.csdn.net/weixin_58196051/article/details/143303049
版权归原作者 i道i 所有, 如有侵权,请联系我们删除。

“python 爬虫 入门 六、Selenium”的评论:

还没有评论