用python+selenium将腾讯首页今日话题的内容自动发表到自己cnblog里
目的:使用pyhton下的unittest单元测试框架并结合selenium的webdriver来实现将腾讯首页的今日话题下的内容自动发表达到自己的cnblog里。
思路:创建QQDailyTopic类继承unittest的TestCase类,setUp()方法用于测试执行前的初始化工作,而最后的tearDown()与setUp()方法相呼应,用于测试执行之后的善后工作。
然后用方法get_qq_daily_topic_url获得qq首页今日话题的url;
方法get_title_and_content_from_qq_daily_topic从今日话题获得后面我们在cnblog下新blog需要的标题title和富文本content,提取今日话题title文本和内容的innerHTML;
方法login用于登录,定位元素并输入username和password然后点击登录按钮;
方法set_content用于向富文本框填入内容,这里借助js向添加新随笔的富文本框插入指定内容,通过scecute_scrit()执行js代码;
方法test_transpond_qq_daily_topic用来测试向cnblog里转发qq首页今日话题,包括自动用有效用户名和密码登录我的博客,在添加新随笔的标题和富文本框里自动填入今日话题的标题和内容,最后点击发布按钮。unittest下必须以“test”开头的方法。
基于python 3.x 和 selenium 2
实现代码如下:
#coding=utf-8
from selenium import webdriver
import unittest
from time import sleep
class QQDailyTopic(unittest.TestCase):
def setUp(self):
self.dr = webdriver.Firefox()
self.title, self.content = self.get_title_and_content_from_qq_daily_topic()
def get_qq_daily_topic_url(self):
return self.dr.find_element_by_css_selector('#todaytop a').get_attribute('href')
def get_title_and_content_from_qq_daily_topic(self):
self.dr.get('http://www.qq.com/')
url = self.get_qq_daily_topic_url()
self.dr.get(url)
title = self.dr.find_element_by_id('sharetitle').text
content = self.dr.find_element_by_id('articleContent').get_attribute('innerHTML')
return (title, content)
def login(self, username, password):
self.dr.find_element_by_id('input1').send_keys(username)
self.dr.find_element_by_id('input2').send_keys(password)
self.dr.find_element_by_id('signin').click()
#借助js向添加新随笔的富文本框插入指定内容
def set_content(self, text):
text = text.strip()
js = 'document.getElementById("Editor_Edit_EditorBody_ifr").contentWindow.document.body.innerHTML=\'%s\'' %(text)
print(js)
self.dr.execute_script(js)
def test_transpond_qq_daily_topic(self):
self.dr.get('https://passport.cnblogs.com/user/signin')
self.login('kemi_xxxx', 'kemi_xxxx')#自己博客园用户名和密码
sleep(3)
self.dr.get('https://i.cnblogs.com/EditPosts.aspx?opt=1')
self.dr.find_element_by_id('Editor_Edit_txbTitle').send_keys(self.title)
self.set_content(self.content)
self.dr.find_element_by_id('Editor_Edit_lkbPost').click()
def tearDown(self):
sleep(5)
self.dr.quit()
if __name__ == '__main__':
unittest.main()
实现效果如下:
QQ首页的今日话题

cnblog转发今日话题
用python+selenium将腾讯首页今日话题的内容自动发表到自己cnblog里的更多相关文章
- 在CentOS下利用Python+selenium获取腾讯首页的今日话题。
1.安装依赖包 yum install wget firefox gcc zlib zlib-devel Xvfb 2.安装setuptools 官网地址:https://pypi.python.or ...
- 用python+selenium抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答并保存至html文件
抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答,保存至html文件,该html文件的文件名应该是20160228_zhihu_today_hot.html,也就是日期+zhihu_toda ...
- python+selenium+PhantomJS爬取网页动态加载内容
一般我们使用python的第三方库requests及框架scrapy来爬取网上的资源,但是设计javascript渲染的页面却不能抓取,此时,我们使用web自动化测试化工具Selenium+无界面浏览 ...
- Python+Selenium爬虫实战一《将QQ今日话题发布到个人博客》
前提条件: 1.使用Wamp Server部署WordPress个人博客,网上资料较多,这里不过多介绍 思路: 1.首先qq.com首页获取到今日话题的的链接: 2.通过今日话题链接访问到今日话题,并 ...
- Python+ Selenium自动化登录腾讯QQ邮箱实例
学习了Python语言一段时间后,在公司的项目里也使用到了python来写测试脚本,一些重复的操作都使用脚本来处理了.大大的提高工作效率,减少了一些手工重复的操作. 以下是使用unittest框架写的 ...
- 看我怎么扒掉CSDN首页的底裤(python selenium+phantomjs爬取CSDN首页内容)
这里只是学习一下动态加载页面内容的抓取,并不适用于所有的页面. 使用到的工具就是python selenium和phantomjs,另外调试的时候还用了firefox的geckodriver.exe. ...
- 腾讯大牛教你简单的自动化测试模型(Python+Selenium)
今天讲解简单的自动化测试模型,对于刚接触自动化测试的同学,由于没有编程语言的基础,是搞不懂代码里面的函数.封装.包以及其他概念,只是了解字符串.数组.元组及字典这种最基本的名词,更不懂自动化测试框架了 ...
- Python Selenium设计模式-POM
前言 本文就python selenium自动化测试实践中所需要的POM设计模式进行分享,以便大家在实践中对POM的特点.应用场景和核心思想有一定的理解和掌握. 为什么要用POM 基于python s ...
- python selenium 自动化测试web
如何使用python完成自动化测试web页面呢?首选selenium 那基于python的selenium如何使用,下面看一段测试案例: 基于python的selenium 安装方法: pip i ...
随机推荐
- 查看占用cpu和内存最多的进程
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -;ps aux|grep -v PID|sort -rn -k +|head linux下获取占用内存 ...
- How To Use DBLink In Oracle Forms 6i
You want to connect multiple databases in oracle forms to perform certain tasks, for example you nee ...
- 关于JS中apply方法的基本理解
最近研究OpenLayers源码时,发现其中使用了比较多的apply方法,对其也是很不明白.于是上网经过多方面了解以及自己细细体会后,终于算是基本明白是其干什么的了,这里分享下.apply方法的造型是 ...
- [Prodinner项目]学习分享_第三部分_Service层(业务逻辑层)
前两节讲到怎样生成一个Model和怎样将Model映射到数据库,这一节将讲到业务逻辑层,也就是Service层. 1.Prodinner架构已经构建好的,基本的增删改查. 假设,我现在想操作第二节中讲 ...
- XMLHttpRequest upload属性
一.新版本的XMLHttpRequest对象,传送数据的时候,有一个progress事件,用来返回进度信息. 它分成上传和下载两种情况 1)下载的progress事件属于XMLHttpRequest对 ...
- 【转】Linux系统启动过程分析
[转]Linux系统启动过程分析 转自:http://blog.chinaunix.net/uid-23069658-id-3142047.html 经过对Linux系统有了一定了解和熟悉后,想对其更 ...
- 【转载】jQuery1.5之后的deferred对象详解
原文:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 原文作者 ...
- remount failed: Operation not permitted ,怎么办呢?
remount failed: Operation not permitted ,怎么办呢? 1. 确定是否正确连接手机了$ adb devices 2. 进入shell$ adb shell 3. ...
- UIDatePicker的用法
目录[-] 1.Locale 2.Calendar 3.timeZone 4.date 5.minimumDate 6.maximumDate 7.countDownDuration 8.minute ...
- Find a point on a 'line' between two Vector3
Find a point on a 'line' between two Vector3http://forum.unity3d.com/threads/find-a-point-on-a-line- ...