本文是在上一篇文章 使用Python自動(dòng)化發(fā)布文章:(一) 基礎(chǔ)上加以改進(jìn)而來(lái)的. 可以完成自動(dòng)化發(fā)布文章。 操作演示: 我先說(shuō)說(shuō)文章怎么發(fā)布.
完整代碼# coding:utf-8from selenium.webdriver.common.action_chains import ActionChainsfrom selenium import webdriverimport timeimport sysreload(sys)sys.setdefaultencoding('utf-8')class autoWrite(object): def __init__(self): ''' 初始化 ''' self.sysType = {} self.title = '' self.articleType = '' self.content = '' self.init_type() self.read_file() pass def init_type(self): ''' 初始化文件類(lèi)型 :return: ''' self.sysType['移動(dòng)開(kāi)發(fā)'] = '428602' self.sysType['前端開(kāi)發(fā)'] = '428612' self.sysType['人工智能'] = '5611447' self.sysType['服務(wù)端開(kāi)發(fā)/管理'] = '428640' self.sysType['游戲開(kāi)發(fā)'] = '429511' self.sysType['編程語(yǔ)言'] = '428609' self.sysType['數(shù)據(jù)庫(kù)'] = '428610' self.sysType['企業(yè)開(kāi)發(fā)'] = '428611' self.sysType['圖像/多媒體'] = '428647' self.sysType['系統(tǒng)運(yùn)維'] = '428613' self.sysType['軟件工程'] = '428638' self.sysType['大數(shù)據(jù)'] = '5593654' self.sysType['云計(jì)算'] = '428639' self.sysType['開(kāi)源硬件'] = '430884' self.sysType['其他類(lèi)型'] = '430381' pass def read_file(self): ''' 讀取文件部分 :return: ''' with open('english.txt', 'r') as f: # 讀取文件的前兩行 for i in range(2): if i == 0: self.title = f.readline() else: self.articleType = f.readline() self.content = f.read() pass def publish_file(self): ''' 文件發(fā)布 :return: ''' user_main_url = 'https://my.oschina.net/u' username = 'xxx' # 改成你自己的賬號(hào)與密碼 password = 'xxx' url = 'https://www.oschina.net/home/login' driver = webdriver.Firefox() driver.get(url) # 瀏覽器窗口最大化 driver.maximize_window() driver.find_element_by_id('userMail').send_keys(username) driver.find_element_by_id('userPassword').send_keys(password) driver.find_element_by_xpath('//*[@id='account_login']/form/div/div[5]/button').click() time.sleep(2) # 點(diǎn)擊進(jìn)入我的博客 # 懸停時(shí),出現(xiàn)新的塊 hoverControl = driver.find_element_by_xpath('//*[@id='MySpace']') ActionChains(driver).move_to_element(hoverControl).perform() # 點(diǎn)擊進(jìn)入我的博客 driver.find_element_by_xpath('/html/body/header/div/div[2]/div/div[2]/div/ul/li[4]/a').click() time.sleep(2) # print driver.page_source # 點(diǎn)擊進(jìn)入寫(xiě)作頁(yè)。 driver.find_element_by_xpath('/html/body/div/div/div/div/div[1]/div[1]/div[4]/a').click() time.sleep(1) # 填寫(xiě)標(biāo)題 titleInput = driver.find_element_by_xpath('//*[@id='title']') # 這里需要對(duì)文字進(jìn)行重新編碼 title = unicode(self.title, 'utf8') titleInput.send_keys(title) # 填寫(xiě)內(nèi)容 contentInput = driver.find_element_by_id('mdeditor') content = unicode(self.content, 'utf8') contentInput.send_keys(content) #注意這里取的文章文章類(lèi)型,剛才有換行了,所以需要去掉\n articleType = self.articleType.strip('\n') # 選擇文章的類(lèi)型值, js = 'document.getElementById('sys_sort').value='{}';'.format(self.sysType[articleType]) print js driver.execute_script(js) # 修改選擇的類(lèi)型文本 js = 'document.getElementsByClassName('select-show')[2].innerText='{}';'.format(articleType) print js driver.execute_script(js) # 點(diǎn)擊提交 submit = driver.find_element_by_xpath('//*[@id='blog-form']/div[3]/div/button[2]') submit.click() print '發(fā)布成功!' passif __name__ == '__main__': auto = autoWrite() auto.publish_file() 雖然目前已經(jīng)可以自運(yùn)貨發(fā)布文章了,但離商業(yè)應(yīng)用還是有比較大的差距!后續(xù)我會(huì)繼續(xù)改進(jìn) 需要解決以下幾個(gè)問(wèn)題.
|
|