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。转载请注明,变饼档博客