https://blog.csdn.net/a942242856/article/details/88379727

原文地址:http://www.bianbingdang.com/article_detail/148.html

#python-selenium登陆今日头条

在运营今日头条的过程当中,有时候未免要进行一些重复无味的劳动。比如在发放微头条的时候,写好了许多内容,并不像每次登陆然后逐个发表。比如我想每个整点去发表一些东西。那么自动登陆今日头条就很有必要了。

选择selenium

选择这个工具的原因是,它可以模拟浏览器去登陆,从而避免一些不必要的麻烦。比如各种浏览器时间戳验证,反爬虫等不好处理的东西(请求头的拼接、cookies的获取)。加上运行不是特别的频繁,也不会造成频繁输入验证码、封IP等。

下载selenium驱动

设置浏览器模型

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登陆今日头条的更多相关文章

  1. 服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台

    服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台 服务器端开发(Python/C++)

  2. Python爬取今日头条段子

    刚入门Python爬虫,试了下爬取今日头条官网中的段子,网址为https://www.toutiao.com/ch/essay_joke/源码比较简陋,如下: import requests impo ...

  3. python爬取今日头条关键字图集

    1.访问搜索图集结果,获得json如下(右图为data的一条的详细内容).页面以Ajax呈现,每次请求20个图集,其中 title --- 图集名字 artical_url --- 图集的地址 cou ...

  4. python抓取今日头条

    # 直接上代码,抓取关键词搜索结果的json数据# coding:utf-8 import requests import json url = 'http://www.toutiao.com/sea ...

  5. 用Python爬下今日头条所有美女,美滋滋!

      我们的学习爬虫的动力是什么? 有人可能会说:如果我学好了,我可以找一个高薪的工作. 有人可能会说:我学习编程希望能够为社会做贡献(手动滑稽) 有人可能会说:为了妹子! ..... 其实我们会发现妹 ...

  6. python + selenium登陆并点击百度平台

    from PIL import Imagefrom selenium.webdriver import DesiredCapabilitiesfrom selenium import webdrive ...

  7. python selenium登陆网易云音乐

    from selenium import webdriver import time driver=webdriver.Chrome() driver.get("http://music.1 ...

  8. python爬取今日头条图片

    import requests from urllib.parse import urlencode from requests import codes import os # qianxiao99 ...

  9. python学习(26)分析ajax请求抓取今日头条cosplay小姐姐图片

    分析ajax请求格式,模拟发送http请求,从而获取网页代码,进而分析取出需要的数据和图片.这里分析ajax请求,获取cosplay美女图片. 登陆今日头条,点击搜索,输入cosplay 下面查看浏览 ...

随机推荐

  1. 7 静态分析Android

    静态分析两种方式: 1. 阅读反汇编的Dalvik字节码:使用IDA 分析dex文件或baksmali反编译的smali文件 2. 阅读反汇编的Java源码:使用dex2jar生成jar文件,用jd- ...

  2. mybatis 批量添加数据的两种实现方式

    做开发的这几年期间经常遇到类似处理这种形式数据的问题,也遇到很多刚刚入行的新同学,发现他们做处理这块,经验不够,今天特地整理了一下,大家都会遇到的几种情况,代码也都粘贴出来了,拿去不谢,有时间大家还是 ...

  3. Go编程基础(介绍和安装)

    Michaelhbjian 2018.10.07 19:41 字数 892 阅读 317评论 0喜欢 0 Go(又称Golang[3])是Google开发的一种静态强类型.编译型.并发型,并具有垃圾回 ...

  4. Python与设计模式--工厂模式

    快餐点餐系统 想必大家一定见过类似于麦当劳自助点餐台一类的点餐系统吧.在一个大的触摸显示屏上,有3类可以选择的上餐品:汉堡等主餐.小食.饮料.当我们选择好自己需要的食物,支付完成后,订单就生成了.下面 ...

  5. jenkins中jmeter项目流程图

  6. 搭建unity客户端

    1.新建个unity的项目ChatClient 2.在unity的Main Camera下挂载个脚本PhotonServerEngine做为与服务器端通信的脚本 3.在PhotonServerEngi ...

  7. C++编译器与链接器工作原理

    http://blog.csdn.net/success041000/article/details/6714195 1. 几个概念 1)编译:把源文件中的源代码翻译成机器语言,保存到目标文件中.如果 ...

  8. O2O项目之一 环境搭配

    技术栈:vue全家桶(使用nuxtjs:https://zh.nuxtjs.org/guide/installation  ) + ssr + redis + mongodb + wabpack + ...

  9. 2019阿里JVM组实习面经

    面试质量非常高....非常高...高... 一面 自我介绍 看过hotspot哪些模块,模板解释器工作说一下,生成的native code放在哪,怎么处理safepoint的 说项目,实现了哪些字节码 ...

  10. java 解决nginx代理的跨域访问问题

    一.什么是跨域 跨域是浏览器对JavaScript同源策略的限制 二.什么情况下会产生跨域 域名不同 wwww.baidu.com www.jd.com 域名相同,访问的端口不同 wwww.baidu ...