https://blog.csdn.net/a942242856/article/details/88379727
原文地址:http://www.bianbingdang.com/article_detail/148.html
#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()
- 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。轉載請注明,變餅檔博客