前提条件:
1、使用Wamp Server部署WordPress个人博客,网上资料较多,这里不过多介绍
思路:
1、首先qq.com首页获取到今日话题的的链接;
2、通过今日话题链接访问到今日话题,并获取今日话题的标题以及话题内容;
3、登录到本地的博客,发表今日话题。
难点:
今日话题的内容就是对应文章‘innerHTML’属性的内容
脚本实现如下:
#coding:utf-8
from selenium import webdriver
import time class QQDailyHot(): def __init__(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.title, self.content = self.get_title_and_content_from_daily_hot() def get_daily_hot_url(self):
return self.by_css('#todaytop a').get_attribute('href') def get_title_and_content_from_daily_hot(self):
self.driver.get('http://www.qq.com/')
url = self.get_daily_hot_url()
self.driver.get(url)
title = self.by_id('sharetitle').text
content = self.by_id('articleContent').get_attribute('innerHTML')
return title, content def quit(self):
self.driver.quit() def create_post_from_daily_hot(self):
self.driver.get('http://localhost/wp-login.php')
self.login_as_test()
self.driver.get('http://localhost/wp-admin/post-new.php')
self.by_id('title').send_keys(self.title)
self.set_content(self.content)
self.by_id('publish').click() def set_content(self, text):
text = text.strip()
js = 'document.getElementById("content_ifr").contentWindow.document.body.innerHTML=\'%s\'' %(text)
print(js)
self.driver.execute_script(js) def login(self, user_name, password):
self.by_id('user_login').send_keys(user_name)
self.by_id('user_pass').send_keys(password)
self.by_id('wp-submit').click() def login_as_test(self):
user_name = password = 'test'
self.login(user_name, password) def by_id(self, the_id):
return self.driver.find_element_by_id(the_id) def by_css(self, css):
return self.driver.find_element_by_css_selector(css) def by_name(self, name):
return self.driver.find_element_by_name(name) if __name__ == '__main__':
daily_hot = QQDailyHot()
daily_hot.create_post_from_daily_hot()
daily_hot.quit()
登录到WordPress,查看文章内容(部分截图):