功能

已经实现:实现贴吧自动发帖功能(包括标题,内容,上传图片),标题和内容加入了简单的随机字符串,每发一次会随机等待一段时间,关注人数少于50000的不发。

缺陷:自动登录需要关闭手机验证,且第一次验证码需要手动拖拽,有想法的同学可以买第三方来做验证码自动识别;发帖间隔不宜过快,否则每次发帖都会要求输入验证码。

开始编码

以下部分直接上代码,未做较好的封装,有兴趣的朋友自己优化~

# -*- coding: utf-8 -*-from selenium.webdriver.chrome.options import Optionsfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom time import sleepimport loggingimport sysimport randomimport uuidfrom pywinauto import Applicationfrom pywinauto.keyboard import send_keyslogging.basicConfig(level=logging.INFO,format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s', filemode='w',filename="log.txt")BROWSER_URL = r'D:\program files(x86)\360chrome\Chrome\Application\360chrome.exe'from pywinauto.application import Applicationclass WinAuto:def __init__(self, class_name, title_re):# 连接到指定应用程序,此处为连接到指定窗口self.app = Application().connect(class_name=class_name,title_re=title_re)# 定位窗口方法def get_window(self, window_object, class_name="", title_re=""):return window_object.window(class_name=class_name,title_re=title_re)# 向编辑框输入指定信息def file_input(self, file_path):# 定位到标题名为“打开”对话框window = self.get_window(self.app, "#32770", "打开")# 定位到编辑框window = self.get_window(window, class_name="Edit")# 向编辑框中输入信息window.TypeKeys(file_path)# 点击【打开】按钮def open_button_click(self):# 定位到标题名为“打开”对话框window = self.get_window(self.app, "#32770", "打开")# 定位到【打开】按钮button = self.get_window(window, class_name="Button", title_re="打开")# 点击【打开】按钮button.click()def get_titile():title_list = [

一个用心码了这么多文字的人,往往渴望得到大家的认可。如果你觉得这篇文章对你有帮助,双击屏幕,给我点个赞呀!

更多软件测试资源分享

软件测试技术交流群

]t = random.choice(title_list)return t + str(uuid.uuid4()).split('-')[-1]def get_content():content ="""testest{uuid}atetset{uuid}testest{uuid}test""".format(uuid=str(uuid.uuid4()).split('-')[-1])return contentdef get_datas():with open('tieba.txt', 'r', encoding='utf-8', errors='ignore') as fp:schools = fp.readlines()title = get_titile()content = get_content()return title, content, schoolsdef get_sleep_time(num):if num % 5 == 0:t = random.choice(range(180, 300))else:t = random.choice(range(35, 125))logging.info("sleep {}".format(t))return tdef start_chrome():chrome_options = Options()chrome_options.binary_location = BROWSER_URL# chrome_options.add_argument("–incognito")driver = webdriver.Chrome(options=chrome_options)driver.implicitly_wait(20)if login_tieba(driver):title, content, schools = get_datas()num = 0for name in schools:url = u"https://tieba.baidu.com/f?ie=utf-8&kw=%s&fr=search" % namelogging.info("start request {}, url:{}".format(name, url))try:driver.get(url)except Exception as e:logging.error("request {} error".format(name))continuefollowers = driver.find_element_by_class_name("card_menNum").textlogging.info("followers is :{}".format(followers))followers = followers.replace(',', "")if int(followers) < 50000:logging.info("Less than 50000 followers")continuetry:driver.find_element_by_xpath("/html/body/ul/li[2]/a").click()sleep(1)driver.find_element_by_xpath('//*[@id="tb_rich_poster"]/div[3]/div[1]/div[2]/input').send_keys(title)# driver.quit()sleep(1)driver.find_element_by_id("ueditor_replace").send_keys(content)sleep(1)driver.find_element_by_class_name('edui-btn-image').click()sleep(1)driver.find_element_by_class_name('pic_upload_container').click()sleep(1)driver.find_element_by_class_name('next_step').click()sleep(1)# 定位打开窗口window = WinAuto("#32770", "打开")sleep(1)if num % 2 == 0:window.file_input(r'F:\py_home\selenium_auto\2.jpg')else:window.file_input(r'F:\py_home\selenium_auto\3.jpg')sleep(1)window.open_button_click()sleep(8)driver.find_element_by_link_text('插入图片').click()sleep(5)driver.find_element_by_xpath('//*[@id="tb_rich_poster"]/div[3]/div[5]/div/button[1]').click() except Exception as e:logging.error("post article failed,: {}".format(e))import tracebacktraceback.print_exc()continuenum += 1t = get_sleep_time(num)sleep(t)driver.quit()def login_tieba(driver):tieba_url = 'https://tieba.baidu.com/'driver.get(tieba_url)driver.find_element_by_xpath('//*[@id="com_userbar"]/ul/li[4]/div/a').click()sleep(2)try:driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__footerULoginBtn"]').click()driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__userName"]').send_keys('用户名')driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__password"]').send_keys('密码')driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__submitWrapper"]').click()logging.info("login success")except Exception:logging.error("login failed")sys.exit(1)logging.info("wait for 10s")sleep(10)return Trueif __name__ == '__main__':start_chrome()

Python+Selenium,实现贴吧自动发帖相关推荐

  1. Python+Selenium自动化测试——126邮箱自动登录脚本(登录首页是二维码,切入账号密码输入框)

    Python+Selenium自动化测试--126邮箱自动登录脚本 版权声明:本文为博主原创文章,未经允许不得转载.https://blog.csdn.net/qiao_wan/article/det ...

  2. python + selenium 实现 问卷星自动抢讲座

    python + selenium 实现 问卷星自动抢讲座,抢不到讲座的同学可以借鉴一下 这是我写的代码 from selenium import webdriver from selenium.we ...

  3. python+selenium 实现 问卷星自动抢讲座

    python + selenium 实现 问卷星自动抢讲座 文章目录 python + selenium 实现 问卷星自动抢讲座 一.代码 二.解析 1.安装selenium库 1)PyCharm 编 ...

  4. 用Python+Selenium来测试登录自动登录163邮箱

    用Python+Selenium来测试登录自动登录163邮箱 1 准备条件 2 程序说明 3 代码 1 准备条件 ① windows系统(也许你是Linux,Mac,自行选择) ②安装了Anacond ...

  5. 利用python+selenium带上cookies自动登录bilibili

    利用python+selenium带上cookies自动登录bilibili 环境 selenium的安装 思路 第一部分:手动登录,保存cookies 第二部分:读取cookies自动登录 环境 环 ...

  6. Python+Selenium,让浏览器自动帮你下文献

    在做学术.搞科研的过程中,我们往往需要针对一个特定的主题下载海量的文献.在把几百篇文献下载到电脑的过程中,假如遇到不够友好的数据库不提供批量下载的功能,怎么办?我恰好遇到了这样的批量下载的科研任务和批 ...

  7. python webdriver 等待网页已登录_python基础编程:python+selenium实现163邮箱自动登陆的方法...

    本文介绍了 让我们先来预览一下代码运行效果吧: 首先分析163邮箱登陆页面的网页结构(按F12或单击鼠标右键选择审查元素) 1.定位到登陆框(注意登录框是一个iframe,如果不定位到iframe的话 ...

  8. python+selenium百度贴吧自动签到

    #-*- coding:utf-8 -*- from selenium import webdriver import time import os import random from seleni ...

  9. Python+selenium+360浏览器实现自动测试

    最近,因工作需要爬取一个系统的数据.该系统只能用低版本IE浏览器,所以尝试了用360浏览器,考虑能够让更多人扩展该爬虫,选取selenium驱动浏览器.现在把需要用到的主要技术点整理如下,备以后查询. ...

  10. python selenium自动化,Firefox自动下载文件以及浏览器相关配置

    from selenium import webdriver from selenium.common.exceptions import WebDriverException from seleni ...

最新文章

  1. 从源码分析DEARGUI之add_listbox
  2. 全部python编程语言-可以用 Python 编程语言做哪些神奇好玩的事情?
  3. C语言循环求出2的10次幂,C语言求2的100次幂的结果.doc
  4. 一致性设计,而不是一致性
  5. Python中两个浮点数的简单运算
  6. python数据挖掘与分析实战pdf_《Python数据分析与挖掘实战》PDF+完整源码
  7. 小米投资偏爱智能与芯片 雷军:有3家科创板上市
  8. OpenShift 4 之脚本化部署Istio的HelloWorld和BookInfo示例
  9. CAS在Tomcat中实现单点登录
  10. springboot整合PageHelper实现分页效果
  11. 办公搜索利器UTOOLS-基于EVERYTHING的文件快速搜索软件
  12. echarts源码打包_Echarts模块v1.5更新【更新支持多线程,封装大量快速方法,增加史上最详细示例】...
  13. 封装0805跟0603有什么区别,只存在大小的区别么???
  14. 富士通Fujitsu DPK2089K 打印机驱动
  15. JAVA 毕业设计 论文题目参考
  16. robots.txt文件信息泄漏
  17. 了解5G技术与未来5G面临的问题
  18. 保护 iOS 用户数据安全: Keychain 和 Touch ID
  19. FTPC 在制品跟踪(WIP Tracking)对象
  20. 斜杠'/' 和反斜杠'\'

热门文章

  1. 使用hibernate tools插件生成POJO
  2. Shell脚本检测容器是否运行
  3. 微软确立新模式:付费Win10成现金流
  4. 我的世界服务器无限装备指令2020,我的世界指令生成器
  5. python连接sql server数据库(pyodbc)
  6. 全志h3linux移植教程,全志H3_wifi模块移植说明文档V-SDK软件资料
  7. html没有网络也可以打开,我的html网页不能正常打开?
  8. r77-Rootkit后渗透技战术分析
  9. 如何搭建高德离线地图服务?
  10. 信息、消息与信号及通信系统的组成