python-selenium登陆今日头条
https://blog.csdn.net/a942242856/article/details/88379727
原文地址:http://www.bianbingdang.com/article_detail/148.html
#python-selenium登陆今日头条
在运营今日头条的过程当中,有时候未免要进行一些重复无味的劳动。比如在发放微头条的时候,写好了许多内容,并不像每次登陆然后逐个发表。比如我想每个整点去发表一些东西。那么自动登陆今日头条就很有必要了。
选择selenium
选择这个工具的原因是,它可以模拟浏览器去登陆,从而避免一些不必要的麻烦。比如各种浏览器时间戳验证,反爬虫等不好处理的东西(请求头的拼接、cookies的获取)。加上运行不是特别的频繁,也不会造成频繁输入验证码、封IP等。
下载selenium驱动
在谷歌浏览器顶端地址栏输入
chrome://settings/help
打开帮助,查看谷歌浏览器版本
在谷歌官方下载对应的浏览器驱动。
http://chromedriver.storage.googleapis.com/index.html
如果上面的地址进不去,可以选择
https://npm.taobao.org/mirrors/chromedriver将下载下来的驱动放置到,chrome浏览器根目录,并将此目录配置到windows的环境变量当中。

设置浏览器模型
from selenium import webdriver
browser = webdriver.Chrome()
- 1
- 2
获取cookies
browser.get("https://mp.toutiao.com")
# 点击登陆按钮
login = browser.find_element_by_css_selector('body > div > div.carousel > div.page.page-1 > div > img.i3')
login.click()
time.sleep(3)
# 填写手机号
phone = browser.find_element_by_id('user-name')
phone.send_keys('19991320539')
# 获取验证码
browser.find_element_by_id('mobile-code-get').click()
verfiy_code_input = input("请输入验证码:")
# 验证码输入框
mobile_code = browser.find_element_by_id('mobile-code')
mobile_code.send_keys(verfiy_code_input)
# 登陆
browser.find_element_by_id('bytedance-SubmitStatic').click()
time.sleep(5)
cookies = browser.get_cookies()
with open('cookies.json', 'w') as f:
self.cookies = json.loads(f.write(json.dumps(cookies)))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
这块将获取到cookies放到cookies.json文件当中,这块今日头条在第一次登陆,会有一个云验证的图片,这块比较麻烦,只等手动点击,来获取到cookies。但是获取到之后,官方默认可以保持一个月。所以这块比较放心,不用每次都去登陆,只要得到cookie就行
使用cookie登陆
browser.get("https://mp.toutiao.com/profile_v3/index")
with open('cookies.json') as f:
cookies = json.loads(f.read())
for cookie in cookies:
browser.add_cookie(cookie)
- 1
- 2
- 3
- 4
- 5
这块在登陆的时候,可能页面显示未登录,其实设置cookies之后,已经登陆成功了,只需要再刷新以下一下页面 。
可再登陆完成后执行如下代码几次
browser.refresh()
browser.refresh()
- 1
- 2
完整dome代码如下
"""
#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author:'手机视界&[变饼档博客](http://www.bianbingdang.com "变饼档博客")'
@file: login.py
@time: 2019/03/10
"""
import time
import json
from selenium import webdriver
class TouTiao:
def __init__(self):
self.cookies = None
self.browser = webdriver.Chrome()
def set_cookies(self):
with open('cookies.json') as f:
self.cookies = json.loads(f.read())
for cookie in self.cookies:
self.browser.add_cookie(cookie)
def create_session(self):
self.browser.get("https://mp.toutiao.com")
if self.cookies is None:
self.set_cookies()
time.sleep(1)
self.browser.get("https://mp.toutiao.com/profile_v3/index")
def forward_wei(self, content):
"""
跳转微头条
:return:
"""
self.browser.get("https://mp.toutiao.com/profile_v3/weitoutiao/publish")
time.sleep(1)
# 微头条内容框
weitoutiao_content = self.browser.find_element_by_css_selector(
"div > div.garr-container-white.weitoutiao-index-zone > div > div:nth-child(1) > textarea")
weitoutiao_content.send_keys(content)
# 微头条发布按钮
weitoutiao_send = self.browser.find_element_by_css_selector(
"div > div.garr-container-white.weitoutiao-index-zone > div > button")
weitoutiao_send.click()
def login(self):
self.browser.get("https://mp.toutiao.com/profile_v3/index")
# 点击登陆按钮
login = self.browser.find_element_by_css_selector('body > div > div.carousel > div.page.page-1 > div > img.i3')
login.click()
time.sleep(3)
# 填写手机号
phone = self.browser.find_element_by_id('user-name')
phone.send_keys('19991320539')
# 获取验证码
self.browser.find_element_by_id('mobile-code-get').click()
verfiy_code_input = input("请输入验证码:")
# 验证码输入框
mobile_code = self.browser.find_element_by_id('mobile-code')
mobile_code.send_keys(verfiy_code_input)
# 登陆
self.browser.find_element_by_id('bytedance-SubmitStatic').click()
time.sleep(5)
cookies = self.browser.get_cookies()
with open('cookies.json', 'w') as f:
self.cookies = json.loads(f.write(json.dumps(cookies)))
print(cookies, "登陆成功")
def close(self):
self.browser.close()
if __name__ == '__main__':
tou_tiao = TouTiao()
tou_tiao.create_session()
tou_tiao.forward_wei('<br/>test')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
作者微信:bianbingdang。转载请注明,变饼档博客
python-selenium登陆今日头条的更多相关文章
- 服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台
服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台 服务器端开发(Python/C++)
- Python爬取今日头条段子
刚入门Python爬虫,试了下爬取今日头条官网中的段子,网址为https://www.toutiao.com/ch/essay_joke/源码比较简陋,如下: import requests impo ...
- python爬取今日头条关键字图集
1.访问搜索图集结果,获得json如下(右图为data的一条的详细内容).页面以Ajax呈现,每次请求20个图集,其中 title --- 图集名字 artical_url --- 图集的地址 cou ...
- python抓取今日头条
# 直接上代码,抓取关键词搜索结果的json数据# coding:utf-8 import requests import json url = 'http://www.toutiao.com/sea ...
- 用Python爬下今日头条所有美女,美滋滋!
我们的学习爬虫的动力是什么? 有人可能会说:如果我学好了,我可以找一个高薪的工作. 有人可能会说:我学习编程希望能够为社会做贡献(手动滑稽) 有人可能会说:为了妹子! ..... 其实我们会发现妹 ...
- python + selenium登陆并点击百度平台
from PIL import Imagefrom selenium.webdriver import DesiredCapabilitiesfrom selenium import webdrive ...
- python selenium登陆网易云音乐
from selenium import webdriver import time driver=webdriver.Chrome() driver.get("http://music.1 ...
- python爬取今日头条图片
import requests from urllib.parse import urlencode from requests import codes import os # qianxiao99 ...
- python学习(26)分析ajax请求抓取今日头条cosplay小姐姐图片
分析ajax请求格式,模拟发送http请求,从而获取网页代码,进而分析取出需要的数据和图片.这里分析ajax请求,获取cosplay美女图片. 登陆今日头条,点击搜索,输入cosplay 下面查看浏览 ...
随机推荐
- 虚拟机-VMware小结
1.网卡的3种模式 桥接模式:虚拟机=物理机器,连接物理网卡,虚拟ip设置物理网卡的网段和网管.可上网. NAT模式:虚拟机把物理机器当做路由器,虚拟ip网段ip自动获取.可上网. https://w ...
- MySQL 自带的4个系统数据库的说明
自带的4个系统数据库:information_schema.mysql.performance_schema.sys: information_schema:这个数据库保存了mysql服务器所有数据库 ...
- zabbix--源码安装报错总结
源码安装 zabbix 报错总结 1)报错: configure: error: MySQL library not found 解决办法 # find / -name "mysql_con ...
- Veritas NetBackup 8.1.2 升级的主要理由--附升级兼容性检查网址
管理更简单 NetBackup™ 8.1.2 基于 Web 的全新直观用户界面让操作变得极度简单化,最常用操作现在只需单击几次或触摸几下即可完 成.通过台式机或移动设备可为不同角色的其他用户授予访问权 ...
- 纯数据结构Java实现(8/11)(Trie)
欢迎访问我的自建博客: CH-YK Blog.
- Java8新特性(1)—— Stream集合运算流入门学习
废话,写在前面 好久没写博客了,懒了,以后自觉写写博客,每周两三篇吧! 简单记录自己的学习经历,算是对自己的一点小小的督促! Java8的新特性很多,比如流处理在工作中看到很多的地方都在用,是时候扔掉 ...
- wordpress如何防止url被篡改
一位网友反馈说他的wordpress网站经常被篡改url,访问网站直接跳到不相关的页面,只能进入数据库那修改wp_option表中修改homeurl字段才能恢复.如果不知道原理就只能恢复数据库甚至重新 ...
- wordpress去掉自定义菜单的外层div
wordpress调用自定义菜单时自动会在外层加一个<div class="menu-nav-container">,如下图所示,nav是后台定义的菜单名称,如果想把这 ...
- applyMiddleware 沉思录
let newStore = applyMiddleware(mid1, mid2, mid3, ...)(createStore)(reducer, null); 给({ getState, dis ...
- pure funtion
A function is called pure function if it always returns the same result for same argument values and ...