使用python的selenium自动化登录获取cookie

桃扇骨 2021-09-29 10:14 614阅读 0赞

要爬取广点通广告平台数据,这平台居然没有提供开发用的API数据接口,登录是QQ登录,很复杂,不好抓取登录接口逻辑

于是就用这个

想录gif的不好录 -

文档:

  1. https://www.yiibai.com/selenium/selenium_webdriver.html

python扩展

  1. https://pypi.org/project/selenium/

selenium的python官方手册:

  1. https://selenium-python-zh.readthedocs.io/en/latest/index.html

代码:

  1. #!/usr/local/bin/python
  2. # -*- coding: UTF-8 -*-
  3. from selenium import webdriver # 从selenium导入webdriver
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.support import expected_conditions as EC
  7. from selenium.webdriver.chrome.options import Options
  8. import json
  9. import time
  10. chrome_options = Options()
  11. chrome_options.add_argument('--no-sandbox')
  12. chrome_options.add_argument('--disable-dev-shm-usage')
  13. # 不启动界面显示- linux下命令行模式必须启用
  14. # chrome_options.add_argument('--headless')
  15. driver = webdriver.Chrome(chrome_options=chrome_options) # Optional argument, if not specified will search path.
  16. driver.get('http://adnet.qq.com/index') # 获取百度页面
  17. driver.switch_to.frame('ptlogin_iframe') # 进入iframe
  18. # 选择账号密码登录
  19. selElement = driver.find_element_by_id('switcher_plogin')
  20. selElement.click()
  21. # 输入账号密码
  22. userElement = driver.find_element_by_id('u')
  23. pwdButton = driver.find_element_by_id('p') #密码输入框
  24. subButton = driver.find_element_by_id('login_button') #密码输入框
  25. userElement.send_keys("111") #输入框输入
  26. pwdButton.send_keys("xxx") #输入框输入
  27. subButton.click()
  28. # 显示等待
  29. try:
  30. element = WebDriverWait(driver, 10).until(
  31. EC.presence_of_element_located((By.ID, "page"))
  32. )
  33. finally:
  34. cookies = driver.get_cookies()
  35. # time.sleep(5)
  36. # driver.refresh('http://adnet.qq.com/index')
  37. with open("cookies.txt", "w") as fp:
  38. json.dump(cookies, fp)
  39. # 关闭浏览器
  40. driver.close()

很简洁吧,真真实实能解决登录这一块的问题,但是linux上环境的搭建真滴不容易

Linux环境搭建

大致流程

1、python

2、pip selenium

3、chrome brower

4、/usr/bin/chromedriver

参考博客:

  1. Linux配置Selenium+Chrome+Python实现自动化测试:
  2. http://zhaoyabei.github.io/2016/08/29/Linux%E9%85%8D%E7%BD%AESelenium+Chrome+Python%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/

安装chrom-linux去官网:

  1. https://www.chrome64bit.com/index.php/google-chrome-64-bit-for-linux

下载deb文件到机器上

  1. dpkg -i google-chrome-stable_current_amd64.deb

但是报很多的依赖问题

ContractedBlock.gif ExpandedBlockStart.gif

  1. Unpacking google-chrome-stable (72.0.3626.121-1) ...
  2. dpkg: dependency problems prevent configuration of google-chrome-stable:
  3. google-chrome-stable depends on fonts-liberation; however:
  4. Package fonts-liberation is not installed.
  5. google-chrome-stable depends on libappindicator3-1; however:
  6. Package libappindicator3-1 is not installed.
  7. google-chrome-stable depends on libasound2 (>= 1.0.16); however:
  8. Package libasound2 is not installed.
  9. google-chrome-stable depends on libatk-bridge2.0-0 (>= 2.5.3); however:
  10. Package libatk-bridge2.0-0 is not installed.
  11. google-chrome-stable depends on libatk1.0-0 (>= 2.2.0); however:
  12. Package libatk1.0-0 is not installed.
  13. google-chrome-stable depends on libatspi2.0-0 (>= 2.9.90); however:
  14. Package libatspi2.0-0 is not installed.
  15. google-chrome-stable depends on libcairo2 (>= 1.6.0); however:
  16. Package libcairo2 is not installed.
  17. google-chrome-stable depends on libgdk-pixbuf2.0-0 (>= 2.22.0); however:
  18. Package libgdk-pixbuf2.0-0 is not installed.
  19. google-chrome-stable depends on libgtk-3-0 (>= 3.9.10); however:
  20. Package l
  21. dpkg: error processing package google-chrome-stable (--install):
  22. dependency problems - leaving unconfigured
  23. Processing triggers for mime-support (3.59ubuntu1) ...
  24. Processing triggers for man-db (2.7.5-1) ...
  25. Errors were encountered while processing:
  26. google-chrome-stable
  27. sudo apt-get -f install

解决依赖问题

查看是否已经安装:

  1. which google-chrome

转载于:https://www.cnblogs.com/xuweiqiang/p/10439976.html

发表评论

表情:
评论列表 (有 0 条评论,614人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Selenium使用Cookies绕过登录

    在使用selenium测试后台时常常每个流程都需要走登录流程,这样自然比较浪费时间。如果遇到登录需要输入验证码等情况,就可能出师未捷身先死。 在Web应用中,登录状态通常是通