用python+selenium将腾讯首页今日话题的内容自动发表到自己cnblog里
2016-12-30 09:22
435 查看
目的:使用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”开头的方法。
实现代码如下:
cnblog转发今日话题
部分内容截图
实现代码如下:
#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获取腾讯首页的今日话题。
- 看我怎么扒掉CSDN首页的底裤(python selenium+phantomjs爬取CSDN首页内容)
- Python+Selenium爬虫实战一《将QQ今日话题发布到个人博客》
- selenium2+python_用cookie实现自动登录(百度首页为例)
- 自动关闭QQ迷你首页的小程序(加上自己的一些理解)
- Linux 文件内容查看工具介绍--cat,less,more,tail,head//http://blog.sina.com.cn/s/blog_737103810100qsxk.html
- CSDN的blog为何只能在首页发表摘要?
- 更多内容参见:http://smarticcard.blog.sohu.com, http://blog.sina.com.cn/smartcard
- PJ-Blog教程┊增强博客用户体验~让发表内容的同时拷贝到剪贴板以防丢失
- Dede(织梦)发表文章时,内容自动添加关键字链接的方法-详解
- 自己动手写Python实现Ubuntu自动切换壁纸
- 以后新的内容都转向 http://fytzzh.cublog.cn/ 了
- IE8浏览器访问百度首页,输入关键字,点击“百度一下”,搜索内容出现在一个自动打开的新窗口的问题的一个原因
- joomla模板首页无内容时自动隐藏主体内容区域
- winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已解] 望一起讨论)(技术改变世界-cnblog)
- 添加主题(跟我来):跟我做在Hope Club发表自己的主题内容
- 第一次发表自己的Blog
- 搬到自己的空间了http://www.linkstudio.com.cn/xblog/