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. java 8大happen-before原则

    1.单线程happen-before原则:在同一个线程中,书写在前面的操作happen-before后面的操作. 2.锁的happen-before原则:同一个锁的unlock操作happen-bef ...

  2. unittest管理用例生成测试报告

    #登录方法的封装 from appium import webdriver from time import sleep from python_selenium.Slide import swipe ...

  3. Jenkins实用发布与回滚PHP项目生产实践

    目录 1.概述 2.项目实践 2.1.环境说明 2.2.Jenkins配置 2.2.1.修改Jenkins的运行用户 2.2.2.配置Jenkins用户和Gitlab的ssh-key 2.2.3.Je ...

  4. 【功能点】php导出excel

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_33862644/article/d ...

  5. Django 之 rest_framework 分页器使用

    Django rest_framework 之分页器使用以及其源码分析 三种分页方式: 常规分页 -->PageNumberPagination 偏移分页 -->LimitOffsetPa ...

  6. 【解决】Error: ENOSPC: no space left on device, watch

    发现问题: 启动 node 项目ReactNative时候出现报错Error: ENOSPC: no space left on device, watch [root@iz2zeihk6kfcls5 ...

  7. Python决策树可视化:GraphViz's executables not found的解决方法

    参考文献: [1]Python决策树可视化:GraphViz's executables not found的解决方法

  8. Elasticsearch Date类型,时间存储相关说明

    资料 网址 Elasticsearch 插入时间字段时数据格式问题 https://segmentfault.com/a/1190000016296983 Elasticsearch Date类型,时 ...

  9. egg 连接 mysql 的 docker 容器,报错:Client does not support authentication protocol requested by server; consider upgrading MySQL client

    egg 连接 mysql 的 docker 容器,报错:Client does not support authentication protocol requested by server; con ...

  10. centos7离线部署Patroni

    实验环境Centos7.7.1908 x86_64 这里说明下为什么需要安装gcc readline-devel zlib-devel这三个包,因为编译安装postgres需要用到 一.首先安装gcc ...