python-selenium登錄今日頭條

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

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

#python-selenium登錄今日頭條python

在運營今日頭條的過程中,有時候未免要進行一些重複無味的勞動。好比在發放微頭條的時候,寫好了許多內容,並不像每次登錄而後逐個發表。好比我想每一個整點去發表一些東西。那麼自動登錄今日頭條就頗有必要了。web

選擇selenium

選擇這個工具的緣由是,它能夠模擬瀏覽器去登錄,從而避免一些沒必要要的麻煩。好比各類瀏覽器時間戳驗證,反爬蟲等很差處理的東西(請求頭的拼接、cookies的獲取)。加上運行不是特別的頻繁,也不會形成頻繁輸入驗證碼、封IP等。chrome

下載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就行api

使用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。轉載請註明,變餅檔博客

相關文章
相關標籤/搜索