15.selenium_case04
# 用selenium实现一个头条号的模拟发文接口 import time
import redis
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait r = redis.Redis('127.0.0.1', 6379) def toutiao_save_and_preview(title, content, expand_link):
option = webdriver.ChromeOptions()
# option.add_argument('headless')
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', chrome_options=option) # 获取渲染的正文
driver.get('./paste.html')
driver.execute_script("contentIn('"+ content +"');")
timeout = 5
content_copy = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath('//button[@class="btn"]'))
content_copy.click() # 模拟登录发文页面 cookie_toutiao = [{'name': 'ccid', 'value': 'db43e70fd9404338c49209ba04f7a11f'}, {'name': 'tt_webid', 'value': ''}, {'name': 'UM_distinctid', 'value': '1667a53d28d449-0e229246a33996-4a506a-1fa400-1667a53d28e361'}, {'name': 'sso_uid_tt', 'value': '4c8179804d74252717c675607c721602'}, {'name': 'toutiao_sso_user', 'value': '8acc9b248cd201034637248021183d5a'}, {'name': 'sso_login_status', 'value': ''}, {'name': 'sessionid', 'value': '8441fa3fc5ae5bc08c3becc780b5b2df'}, {'name': '_mp_test_key_1', 'value': '6aba81df9e257bea2a99713977f1e33b'}, {'name': 'uid_tt', 'value': '75b5b52039d4c9dd41315d061c833f0b'}, {'name': 'ccid', 'value': '4231c5cd5a98033f2e78336b1809a18a'}, {'name': 'tt_webid', 'value': ''}, {'name': 'UM_distinctid', 'value': '16783e1566479-0ae7bcdcaeb592-113b6653-13c680-16783e156656d4'}, {'name': 'passport_auth_status', 'value': '99f731f2c6dc150e6dfea46799f20e90'}, {'name': 'sso_uid_tt', 'value': 'f4bcd2cf972384b428449b0479475ce6'}, {'name': 'toutiao_sso_user', 'value': '60df7bb620b4b6d1d17a1de83daec9c1'}, {'name': 'sso_login_status', 'value': ''}, {'name': 'sessionid', 'value': '786fe64e9186d51b8427290a557b3c7b'}, {'name': 'uid_tt', 'value': '91a7a72a85861ae686fb66177bc16bca'}, {'name': '__tea_sdk__ssid', 'value': '60b289e6-e2a4-4494-a3e8-7936f9731426'}, {'name': 'uuid', 'value': 'w:3ec91cefd76b438583154fea77baa54b'}, {'name': 'tt_im_token', 'value': ''}] driver.get('https://mp.toutiao.com/profile_v3/index')
for cookie in cookie_toutiao:
driver.add_cookie(cookie)
driver.get('https://mp.toutiao.com/profile_v3/graphic/publish')
print(driver.title) # driver.maximize_window() # 写标题
print('写标题')
write_title = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath('//*[@id="title"]'))
write_title.click()
write_title.send_keys(title) # 粘贴正文
print('写正文')
write_content = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath(
'//*[@id="graphic"]/div/div/div[2]/div[1]/div[2]/div[3]/div[2] | //div[contains(@class,"ql-editor")]'))
write_content.click()
write_content.clear()
write_content.send_keys(Keys.SHIFT + Keys.INSERT)
# time.sleep(1) # 检测图片上传是否完成
try:
if 'img' in content:
WebDriverWait(driver, timeout).until(
lambda d: d.find_element_by_xpath('//div[@class="pgc-img-wrapper"]'))
print('images uploaded success')
else:
print('no image included')
except:
print('images uploaded fail') # 页面向下滚动
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(1) # 添加扩展链接
expand_check = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath(
'//div[@class="pgc-external-link"]//input[@type="checkbox"]',
))
expand_check.click()
expand_link_box = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath(
'//div[@class="link-input"]//input[@type="text"]',
))
expand_link_box.send_keys(expand_link)
time.sleep(1) # 自动封面
front_img = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath(
'//div[@class="article-cover"]/div/div[@class="tui2-radio-group"]/label[3]/div/input',
))
front_img.click()
time.sleep(1) # 保存草稿
save_draft = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath(
'//div[@class="publish-footer"]/button[4]/span'))
save_draft.click()
time.sleep(1) # 从内容管理页,获取预览链接和文章ID
print('get preview_link and article_id')
# driver.refresh()
preview_link = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath(
'//div[@id="article-list"]//div[@class="master-title"][1]/a')).get_attribute('href')
article_id = preview_link.split('=')[-1]
print(preview_link, article_id)
time.sleep(1) content_management = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_link_text('内容管理'))
content_management.click()
time.sleep(1)
driver.refresh()
preview_link = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath(
'//*[@id="article-list"]/div[2]/div/div/div[1]/div/a')).get_attribute('href')
article_id = preview_link.split('=')[-1] index_page = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath('//a[@class="shead_logo"]'))
index_page.click()
driver.get('https://mp.toutiao.com/profile_v3/index') print(r.scard('cookie_pool_toutiao'))
return preview_link, article_id if __name__ == "__main__":
print('start')
start_time = time.time()
title = '狮子王 Circle of Life 345'
content = '<p>奥术大师多</p><p><img class="wscnph" src="https://upload-images.jianshu.io/upload_images/7178691-a023399f84034497.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/528" /></p>'
expand_link = 'https://www.jianshu.com/u/130f76596b02'
img = ''
preview_link, article_id = toutiao_save_and_preview(title, content, expand_link)
print(preview_link)
print(article_id)
finish_time = time.time()
print(finish_time - start_time)
15.selenium_case04的更多相关文章
- .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...
- 15个关于Chrome的开发必备小技巧[译]
谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...
- 15个C++项目列表
实验楼上有很多C++的实战项目,从简单到进阶,学习每个项目都可以掌握相应的知识点. 如果你还是C++新手的话,那么这个C++的项目列表你可以拿去练手实战开发,毕竟学编程动手实践是少不了的! 如果你不知 ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- 在 Ubuntu 15.04 中使用 ubuntu-make、Eclipse 4.4、Java 8 以及 WTP
Ubuntu 今天发布新版本了 其实昨天(2015-04-23)我就看到了 Ubuntu 发布新版本的新闻,下班后回家的第一件事就是访问 Ubuntu 的官网,很可惜,没有提供下载.今天(2015-0 ...
- 企业IT管理员IE11升级指南【15】—— 代理自动配置脚本
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...
- (转)C++语言的15个晦涩特性
原文链接: Evan Wallace 翻译: 伯乐在线- 敏敏 译文链接: http://blog.jobbole.com/54140/ 这个列表收集了 C++ 语言的一些晦涩(Obscure)特 ...
- CSharpGL(15)用GLSL渲染2种类型的文字
CSharpGL(15)用GLSL渲染2种类型的文字 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合 ...
- ABP(现代ASP.NET样板开发框架)系列之15、ABP应用层——应用服务(Application services)
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之15.ABP应用层——应用服务(Application services) ABP是“ASP.NET Boiler ...
随机推荐
- 解决org.hibernate.HibernateException: identifier of an instance of com.ahd.entity.Order was altered from2 to 0
错误信息 严重: Servlet.service() for servlet [springmvc] in context with path [/order] threw exception [Re ...
- python使用gevent实现并发下载器
并发下载原理 import gevent from gevent import monkey import urllib.request monkey.patch_all() def my_downl ...
- react-conponent-hellocynthia
<!DOCTYPE html> <html> <head> <script src="../../build/react.js">& ...
- es6 语法 (Proxy和Reflect 的对比)
{ //原始对象 let obj={ time:'2017-03-11', name:'net', _r:123 }; //(代理商)第一个参数代理对象,第二个参数真正代理的东西 let monito ...
- BZOJ1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2951 Solved: 1293[Submit][Status ...
- zTree 节点文字过多处理方法
zTree setting.view.addDiyDom 方法可以实现自定义控件,指定节点显示内容.因此需要自己实现addDiyDom方法. 如果树节点不显示checkbox ,处理方法为: func ...
- 使用hibernate报错java.lang.ExceptionInInitializerError的处理方法
今天使用hibernate搭建持久层出现一个问题 原因在于 在创建user liberty时同时勾选了System Library(added to the boot class )path
- ArrayMap 和HashMap的区别
在以往android开发中,我们常常用key-value存储数据时,随手就会打出HashMap的代码,当数据量较小时,这种方法还不错还可以,当数据量比较多的时候,如果是PC机上,也还阔以.但是如果使用 ...
- coTurn测试程序之turnutils_stunclient.exe
测试使用方法,作为STUN客户端,仅用于测试STUN服务是否正常运行. 使用coTurn服务启动STUN/TURN服务后,执行以下命令即可: turnutils_stunclient -p 61.18 ...
- Scala隐式转换
package big.data.analyse.scala import java.io.File import scala.io.Source /** * 隐式转换 * Created by zh ...