https://blog.csdn.net/a942242856/article/details/88379727css
原文地址:http://www.bianbingdang.com/article_detail/148.htmlhtml
#python-selenium登錄今日頭條python
在運營今日頭條的過程中,有時候未免要進行一些重複無味的勞動。好比在發放微頭條的時候,寫好了許多內容,並不像每次登錄而後逐個發表。好比我想每一個整點去發表一些東西。那麼自動登錄今日頭條就頗有必要了。web
選擇這個工具的緣由是,它能夠模擬瀏覽器去登錄,從而避免一些沒必要要的麻煩。好比各類瀏覽器時間戳驗證,反爬蟲等很差處理的東西(請求頭的拼接、cookies的獲取)。加上運行不是特別的頻繁,也不會形成頻繁輸入驗證碼、封IP等。chrome
在谷歌瀏覽器頂端地址欄輸入
chrome://settings/help
打開幫助,查看谷歌瀏覽器版本npm
在谷歌官方下載對應的瀏覽器驅動。
http://chromedriver.storage.googleapis.com/index.html
若是上面的地址進不去,能夠選擇
https://npm.taobao.org/mirrors/chromedriverjson
將下載下來的驅動放置到,chrome瀏覽器根目錄,並將此目錄配置到windows的環境變量當中。windows
from selenium import webdriver browser = webdriver.Chrome()
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就行api
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()
""" #!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。轉載請註明,變餅檔博客