python模拟浏览器 使用selenium进入好友QQ空间并留言


Posted in Python onApril 12, 2022

首先下载selenium模块,pip install selenium,下载一个浏览器驱动程序(我这里使用谷歌)。

#导入
#注意python各版本find_element()方法的变化(python3.10)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
# 创建一个模拟浏览器对象,然后通过对象去操作浏览器s=Service("chromedriver.exe")browser=webdriver.Chrome(service=s)

QQ空间默认登录是使用二维码登录,我们要使用账号密码登录注意QQ空间登录框在一个iframe标签里:定位该框架

python模拟浏览器 使用selenium进入好友QQ空间并留言

browser.get('https://qzone.qq.com/')
browser.maximize_window()time.sleep(2)
browser.switch_to.frame('login_frame')
a_tag = browser.find_element(By.ID,"switcher_plogin")
a_tag.click()

 接下来就是输入账号,密码,点击登录

userName_tag = browser.find_element(By.ID,'u')
password_tag =browser.find_element(By.ID,'p')
time.sleep(1)
userName_tag.send_keys('这里是QQ号')
time.sleep(1)
password_tag.send_keys('这里是密码')
time.sleep(1)
btn = browser.find_element(By.ID,'login_button')
btn.click()

 目前实现的效果图

python模拟浏览器 使用selenium进入好友QQ空间并留言

接下来实现的是,进入上边导航栏的好友页面,并定位好友搜索框,向搜索框传递要搜索的好友

python模拟浏览器 使用selenium进入好友QQ空间并留言

 :部分iframe没有id或name,用xpath定位

browser.switch_to.default_content()  # 登陆完后回到默认框架
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="aMyFriends"]').click()
time.sleep(1)
element1 =browser.find_element(By.XPATH,'//[@id="app_container"]/iframe')
browser.switch_to.frame(element1)
ff=browser.find_element(By.XPATH,'//*[@id="qz-search-box-input"]')
ff.send_keys(friend)
time.sleep(1)
browser.switch_to.default_content()
element2=browser.find_element(By.XPATH,'//[@id="app_container"]/iframe')
browser.switch_to.frame(element2)
browser.find_element(By.XPATH,'//*[@id="qz-search-box-result"]/li/div[2]/p').click()
time.sleep(1)
browser.find_element(By.XPATH,'//[@id="mecarewho_list"]/li/div[2]/div[2]/p/a').click()time.sleep(1)
#进入好友的页面

实现效果:

python模拟浏览器 使用selenium进入好友QQ空间并留言

 接下来就是进入好友留言板进行留言

注意的是留言框和发表按钮在不同的frame,发表在外面的一层,仔细查看

windows = browser.window_handles
browser.switch_to.window(windows[-1])
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="friendship_promote_layer"]/table/tbody/tr[1]/td[2]/a').click()
time.sleep(1)
#browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
browser.find_element(By.XPATH,'//*[@id="menuContainer"]/div/ul/li[4]').click()#或者  browser.find_element(By.XPATH,"//div[@id='layBackground']//li[@class = 'menu_item_334']//a[text()='留言板']").click()
time.sleep(3)#进入留言板
browser.switch_to.frame('tgb')
time.sleep(1)
browser.switch_to.frame('veditor1_Iframe')
time.sleep(1)
ff=browser.find_element(By.XPATH,'/html/body')#留言框
ff.send_keys(word)
browser.switch_to.default_content()
browser.switch_to.frame('tgb')
dd=browser.find_element(By.XPATH,'//*[@id="btnPostMsg"]')
dd.click()#确认发表按钮
print("留言成功!!!")
time.sleep(2)
browser.quit()

python模拟浏览器 使用selenium进入好友QQ空间并留言

 python小白,有错误的的地方还请多多指教

完整代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.webdriver import ActionChains
from selenium.webdriver import ChromeOptions
# 请输入好友和留言内容
qq=input('输入自己的QQ号:')
friend = input('请输入好友:')
word = input('请输入留言内容:')
# 创建一个模拟浏览器对象,然后通过对象去操作浏览器
option=ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-gpu')
s=Service("chromedriver.exe")
browser = webdriver.Chrome(service=s,options=option)
browser.get('https://qzone.qq.com/')
browser.maximize_window()
time.sleep(2)
 
browser.switch_to.frame('login_frame')
a_tag = browser.find_element(By.ID,"switcher_plogin")
a_tag.click()
userName_tag = browser.find_element(By.ID,'u')
password_tag =browser.find_element(By.ID,'p')
time.sleep(1)
userName_tag.send_keys(qq)
time.sleep(1)
password_tag.send_keys('此处输入自己的密码')
time.sleep(1)
btn = browser.find_element(By.ID,'login_button')
btn.click()
 
browser.switch_to.default_content()  # 登陆完后回到默认框架
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="aMyFriends"]').click()
time.sleep(1)
element1 =browser.find_element(By.XPATH,'//*[@id="app_container"]/iframe')
browser.switch_to.frame(element1)
ff=browser.find_element(By.XPATH,'//*[@id="qz-search-box-input"]')
ff.send_keys(friend)
time.sleep(1)
browser.switch_to.default_content()
element2 =browser.find_element(By.XPATH,'//*[@id="app_container"]/iframe')
browser.switch_to.frame(element2)
browser.find_element(By.XPATH,'//*[@id="qz-search-box-result"]/li/div[2]/p').click()#难点
time.sleep(1)#搜索ok
browser.find_element(By.XPATH,'//*[@id="mecarewho_list"]/li/div[2]/div[2]/p/a').click()
time.sleep(1)#进入好友
# 获得打开的第一个窗口句柄
windows = browser.window_handles
browser.switch_to.window(windows[-1])
time.sleep(1)
browser.find_element(By.XPATH,'//*[@id="friendship_promote_layer"]/table/tbody/tr[1]/td[2]/a').click()
time.sleep(1)
#browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
browser.find_element(By.XPATH,'//*[@id="menuContainer"]/div/ul/li[4]').click()#或者  browser.find_element(By.XPATH,"//div[@id='layBackground']//li[@class = 'menu_item_334']//a[text()='留言板']").click()
time.sleep(3)#进入留言板
browser.switch_to.frame('tgb')
time.sleep(1)
browser.switch_to.frame('veditor1_Iframe')
time.sleep(1)
ff=browser.find_element(By.XPATH,'/html/body')#留言框
ff.send_keys(word)
browser.switch_to.default_content()
browser.switch_to.frame('tgb')
dd=browser.find_element(By.XPATH,'//*[@id="btnPostMsg"]')
dd.click()#确认发表按钮
print("留言成功!!!")
time.sleep(2)
browser.quit()

到此这篇关于python使用selenium模拟浏览器进入好友QQ空间留言的文章就介绍到这了!

Python 相关文章推荐
python中正则表达式的使用详解
Oct 17 Python
介绍Python的@property装饰器的用法
Apr 28 Python
Python实现网络端口转发和重定向的方法
Sep 19 Python
Python使用正则表达式实现文本替换的方法
Apr 18 Python
Python multiprocess pool模块报错pickling error问题解决方法分析
Mar 20 Python
Python利用matplotlib做图中图及次坐标轴的实例
Jul 08 Python
python中enumerate() 与zip()函数的使用比较实例分析
Sep 03 Python
在Python IDLE 下调用anaconda中的库教程
Mar 09 Python
python实现超级马里奥
Mar 18 Python
tensorflow pb to tflite 精度下降详解
May 25 Python
python 如何用urllib与服务端交互(发送和接收数据)
Mar 04 Python
pyqt5打包成exe可执行文件的方法
May 14 Python
Python安装使用Scrapy框架
Python使用华为API为图像设置多个锚点标签
python实现手机推送 代码也就10行左右
Apr 12 #Python
Python内置包对JSON文件数据进行编码和解码
详细介绍python操作RabbitMq
Python selenium绕过webdriver监测执行javascript
Apr 12 #Python
Pillow图像处理库安装及使用
Apr 12 #Python
You might like
php快速url重写更新版[需php 5.30以上]
2010/04/25 PHP
深入解析Session是否必须依赖Cookie
2013/08/02 PHP
PHP使用imagick读取PDF生成png缩略图的两种方法
2014/03/20 PHP
php给图片添加文字水印方法汇总
2015/08/27 PHP
php中二维数组排序问题方法详解
2015/08/28 PHP
PHP sdk实现在线打包代码示例
2020/12/09 PHP
JQuery判断子iframe何时加载完成解决方案
2013/08/20 Javascript
node.js中的socket.io的广播消息
2014/12/15 Javascript
使用CDN和AJAX加速WordPress中jQuery的加载
2015/12/05 Javascript
jQuery移动端图片上传组件
2016/06/12 Javascript
微信小程序 http请求详细介绍
2016/10/09 Javascript
JQueryEasyUI之DataGrid数据显示
2016/11/23 Javascript
Vue2组件tree实现无限级树形菜单
2017/03/29 Javascript
Bootstrap 响应式实用工具实例详解
2017/03/29 Javascript
jquery点赞功能实现代码 点个赞吧!
2020/05/29 jQuery
简单的Vue SSR的示例代码
2018/01/12 Javascript
微信小程序页面缩放式侧滑效果的实现代码
2018/11/15 Javascript
Vue.js特性Scoped Slots的浅析
2019/02/20 Javascript
Swiper.js实现移动端元素左右滑动
2019/09/08 Javascript
layui点击数据表格添加或删除一行的例子
2019/09/12 Javascript
python 中的list和array的不同之处及转换问题
2018/03/13 Python
python基础教程项目五之虚拟茶话会
2018/04/02 Python
python中的for循环
2018/09/28 Python
python中强大的format函数实例详解
2018/12/05 Python
把vgg-face.mat权重迁移到pytorch模型示例
2019/12/27 Python
Python字符串查找基本操作代码案例
2020/10/27 Python
英国川宁茶官方网站:Twinings茶
2019/05/21 全球购物
甜美蛋糕店创业计划书
2014/01/30 职场文书
党员志愿者活动方案
2014/08/28 职场文书
2014年副班长工作总结
2014/12/10 职场文书
2019消防宣传标语!
2019/07/10 职场文书
《狼牙山五壮士》读后感:宁死不屈,视死如归
2019/08/16 职场文书
Nginx反爬虫策略,防止UA抓取网站
2021/03/31 Servers
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
2021/04/27 Python
Python绘制地图神器folium的新人入门指南
2021/05/23 Python
JS实现九宫格拼图游戏
2022/06/28 Javascript