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. GitHub Vue项目推荐|Vue+Element实现的电商后台管理系统功能丰富

    GitHub Vue项目推荐|mall-admin-web是一个电商后台管理系统的前端项目基于Vue+Element实现 主要包括商品管理.订单管理.会员管理.促销管理.运营管理.内容管理.统计报表. ...

  2. AOP的动态实现cglib和jdk

    动态代理的两种实现以:cglib和jdk,spring的aop(切面)的实现原理就是采用的动态代理技术. 看完代码.动态代理的作用是什么: Proxy类的代码量被固定下来,不会因为业务的逐渐庞大而庞大 ...

  3. VUE 动态菜单管理

    业务场景 不同的用户登录,看到的菜单会不一样,因此需要根据不同人登录的身份去后端获取菜单. 实现思路 1.构建路由 2.从后端构建菜单 3.前端获取菜单 4.前端渲染菜单 1.构建路由. export ...

  4. nginx与php分离搭建

    nginx与php分离搭建 nginx配置 server { listen       80;      定义端口 server_name  www.a.org;    定义域名 location / ...

  5. Netbackup常用命令--bprestore

    bprestore bprestore – 从 NetBackup 服务器还原文件 大纲 bprestore [-A | -B | -rb] [-K] [-l | -H | -y] [-r] [-T] ...

  6. rn 环境搭建

    https://reactnative.cn/docs/next/getting-started.html 搭建开发环境 欢迎使用 React Native!这篇文档会帮助你搭建基本的 React N ...

  7. &和&& 每天学一点linux

    原文:http://www.cnblogs.com/TianFang/archive/2013/01/23/2872645.html & 放在启动参数后面表示设置此进程为后台进程 默认情况下, ...

  8. Python高级函数--filter

    def is_palindrome(n): return str(n) == str(n)[::-1] #前两个‘:’表示整个范围,‘-’表示从后面,‘1’表示数据间隔 output = filter ...

  9. Json断言

    Additionally assert value:添加验证的值,只有勾选了此复选框,才可以在Expected Value中设置期望的值. Match as regular expression:匹配 ...

  10. Python 12 基础知识

    原文:https://www.cnblogs.com/Lee-yl/p/9884055.html 原文:https://blog.csdn.net/juxiangming/article/detail ...