#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()获取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)))这块将获取到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)这块在登陆的时候,可能页面显示未登录,其实设置cookies之后,已经登陆成功了,只需要再刷新以下一下页面 。
可再登陆完成后执行如下代码几次
browser.refresh()
browser.refresh()完整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')作者微信:bianbingdang。转载请注明,变饼档博客