python小技能: 【自动回复社区帖子的留言】搭建selenium与ChromeDriver环境教程(Mac版本)

简介: python小技能: 【自动回复社区帖子的留言】搭建selenium与ChromeDriver环境教程(Mac版本)

引言

  1. 搭建selenium与ChromeDriver环境
  2. 案例:自动回复社区帖子的留言

I 搭建selenium与ChromeDriver环境

  1. 安装python
  2. 下载驱动:chromedriver.chromium.org、http://npm.taobao.org/mirrors/chromedriver
  3. 安装selenium

1.1 安装pip

pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。

pip --version     # Python2.x 版本命令
pip3 --version    # Python3.x 版本命令

在Python2.7的安装包中,easy_install.py是默认安装的,而pip需要我们手动安装。

方式1

sudo easy_install pip

方式2: curl https://bootstrap.pypa.io/pip/2.7/get-pip.py |python

Installing collected packages: pip
  WARNING: The scripts pip, pip2 and pip2.7 are installed in '/Users/mac/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.3.4
#python3的安装方式:curl https://bootstrap.pypa.io/get-pip.py | python3
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   # 下载安装脚本
$ sudo python get-pip.py    # 运行安装脚本

1.2 安装selenium

selenium是一个web自动化工具,selenium测试直接运行在浏览器中,就像是真正的用户在操作一样。支持的浏览器有IE、Firefox、Safari、Google、Opera等。

  • pip3 install selenium

Successfully installed selenium-3.141.0 urllib3-1.25.10

➜  bin git:(master) ✗  pip3 show selenium
Name: selenium
Version: 4.1.0

Python2.7版本安装selenium

在命令行输入python -m pip install -U selenium 导入selenium都会提示错,因为电脑有两个python路径 /usr/local/lib/ /Library/Python/2.7(Mac 自带的)

1.3 下载驱动

  • 从官网chromedriver.chromium.org下载可执行文件即可。

将chromedriver移至/usr/bin目录下,或者其他path目录都行。(/Users/mac/bin) 保证版本和你安装的Chrome的版本一致即可

image.png

➜  ~ cat .bash_profile
export PATH="$HOME/bin:$HOME/code/flutter/bin:$PATH"

测试是否可运行

➜  ~ chromedriver
Starting ChromeDriver 85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

II 案例:自动回复社区留言【Selenuim版本】

仅供交流

脚本1: 保存已登录cookie

脚本2:自动回五星

两个脚本最好在同一目录下

image.png

2.1 保存已登录cookie

运行脚本, 然后登录账号获取并保持已登录cookie

基于python3 获取登录之后的token信息

 python3 ./csdnToken.py
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
import json
browser = webdriver.Chrome()
browser.get("https://passport.csdn.net/newlogin")
flag = True
print("wlogin")
while flag:
    try:
        browser.find_element(By.XPATH,
            "//a[@class='hasAvatar']")
        flag = False
    except NoSuchElementException as e:
        time.sleep(3)
print("save cookie...")
with open('cookie_csdn.txt', 'w', encoding='utf-8') as f:
    json.dump(browser.get_cookies(), f)
# browser.close()
print("exit browser ")

Python 文件操作中的读写模式:open(路径+文件名, 读写模式, 编码)

读写模式:

r :只读

r+ : 读写

w :新建(会对原有文件进行覆盖)

a :追加

b :二进制文件

2.2 回五星和留下信息

遍历帖子的留言,提取URL之后去填写自己帖子的链接和评论信息。

回五星的方法。

def star():
    try:
        browser.find_element(By.CSS_SELECTOR,
            "div.el-rate > span:nth-child(5) > i")
    except NoSuchElementException as e:
        return 0
    collect_button = browser.find_element(By.CSS_SELECTOR,
        "div.el-rate > span:nth-child(5) > i")
    if collect_button.get_attribute("style") == "color: rgb(153, 154, 170);":
        #collect_button.click()
        #webdriver.ActionChains(browser).move_to_element(collect_button).click(collect_button).perform()
        browser.execute_script("arguments[0].click();", collect_button)
        return 1
    else:
        return 2

see also


目录
相关文章
|
1月前
|
JSON C语言 C++
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
63 1
|
1月前
|
存储 安全 API
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
82 0
|
1天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
3天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
21 0
|
4天前
|
前端开发 测试技术 C++
Python自动化测试面试:unittest、pytest与Selenium详解
【4月更文挑战第19天】本文聚焦Python自动化测试面试,重点讨论unittest、pytest和Selenium三大框架。unittest涉及断言、TestSuite和覆盖率报告;易错点包括测试代码冗余和异常处理。pytest涵盖fixtures、参数化测试和插件系统,要注意避免过度依赖unittest特性。Selenium的核心是WebDriver操作、等待策略和测试报告生成,强调智能等待和元素定位策略。掌握这些关键点将有助于提升面试表现。
18 0
|
8天前
|
Python
干货文:在 Mac 中卸载 Python 的方式
干货文:在 Mac 中卸载 Python 的方式
13 1
|
12天前
|
Web App开发 测试技术 网络安全
|
22天前
|
Python Linux iOS开发
使用 Python 打印本机 Mac 地址
使用 Python 打印本机 Mac 地址
8 0
|
24天前
|
Java
Mac环境下反编译apk
Mac环境下反编译apk
17 0
|
1月前
|
存储 算法 数据挖掘
【Python 基础教程 25】全面入门指南:深度解析Python3的命名空间,作用域及变量使用教程
【Python 基础教程 25】全面入门指南:深度解析Python3的命名空间,作用域及变量使用教程
55 0