Python——通过用户cookies访问微博首页

本文介绍了一种使用Selenium和Requests库实现的微博Cookies登录方法。首先通过Selenium模拟登录过程并保存Cookies到本地文件,然后读取Cookies使用Requests库访问微博主页验证其有效性。
摘要由CSDN通过智能技术生成

通过用户cookies访问微博首页

1.登录微博
        self.driver.delete_all_cookies()  # 删除cookies
        self.driver.get(self.url)
        time.sleep(2)
        user = self.wait.until(EC.presence_of_element_located((By.ID,'loginName')))
        pwd = self.wait.until(EC.presence_of_element_located((By.ID, 'loginPassword')))
        submit = self.wait.until(EC.presence_of_element_located((By.ID, 'loginAction')))
        user.send_keys(self.username)
        time.sleep(1)
        pwd.send_keys(self.password)
        time.sleep(1)
        submit.click()
        time.sleep(5)
2.获取cooikes,保存json文件
cookies = self.driver.get_cookies()
cookies_dict = {}
for item in cookies:
    cookies_dict[item.get('name')] = item.get('value')
with open('sina_cookies.TXT','w',encoding='utf-8') as f:
    f.write(json.dumps(cookies_dict, ensure_ascii='False',indent=4))
3..读取json文件,获得cooikes,访问主页
with open('sina_cookies.TXT','r',encoding='utf-8') as f:
            cooikes_dict = json.loads(f.read())
response = requests.get('https://weibo.cn/', cookies = cookies_dict, timeout=5, allow_redirects=False)
        if response.status_code == 200:
            print('用户cookies有效')
4.详细代码
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/4/26 0:24
# @Author  : hyang
# @File    : WeiboCooikes.py
# @Software: 


import time
import json
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait  # 等待元素加载的
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
import requests

class WeiboCooikesLogin(object):
    """
    通过cookies访问微博
    """
    def __init__(self,username, password):
        self.url = 'https://passport.weibo.cn/signin/login'
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()  # 最大化窗口
        self.wait = WebDriverWait(self.driver, 6)
        self.username = username
        self.password = password

    def __del__(self):
        """
         关闭浏览器
        :return: 
        """
        print('close browser')
        self.driver.close()

    def open_url(self):
        """
        打开url登录微博
        :return: 
        """
        self.driver.delete_all_cookies()  # 删除cookies
        self.driver.get(self.url)
        time.sleep(2)
        user = self.wait.until(EC.presence_of_element_located((By.ID,'loginName')))
        pwd = self.wait.until(EC.presence_of_element_located((By.ID, 'loginPassword')))
        submit = self.wait.until(EC.presence_of_element_located((By.ID, 'loginAction')))
        user.send_keys(self.username)
        time.sleep(1)
        pwd.send_keys(self.password)
        time.sleep(1)
        submit.click()
        time.sleep(5)

    def password_error(self):
        """
        判断用户名密码错误
        :return: 
        """
        try:
            return self.wait.until(EC.text_to_be_present_in_element((By.ID,'errorMsg'),'用户名或密码错误'))
        except TimeoutException as e:
            return False

    def login_successful(self):
        """
         获得登录成功标志
        :return: 
        """
        try:
            return self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'drop-title')))

        except TimeoutException as e:
            return False

    def process_cookies(self,cookies):
        """
        处理cookies
        :param cookies: 
        :return: 
        """
        cookies_dict = {}
        for item in cookies:
            cookies_dict[item.get('name')] = item.get('value')
        return cookies_dict

    def save_cookies(self,cookies_dict):
        """
         保存cookies
        :param cookies_dict: 
        :return: 
        """
        with open('sina_cookies.TXT','w',encoding='utf-8') as f:
            f.write(json.dumps(cookies_dict, ensure_ascii='False',indent=4))

    def get_cookies_main(self):
        self.open_url()
        if self.password_error():
            print('用户名或密码错误')
        if self.login_successful():
            print('用户登录成功')
            cookies = self.driver.get_cookies()
            d = self.process_cookies(cookies)
            self.save_cookies(d)
            print('保存用户cookies成功')

    def get_cooikes(self):
        """
        从文件中读取cookies
        :return: 
        """
        with open('sina_cookies.TXT','r',encoding='utf-8') as f:
            cooikes_dict = json.loads(f.read())

        return cooikes_dict

    def login_with_cookies(self, cookies_dict):
        """
        通过cookies访问主页读取信息
        :param cookies_dict: 
        :return: 
        """
        time.sleep(2)
        response = requests.get('https://weibo.cn/', cookies = cookies_dict, timeout=5, allow_redirects=False)
        if response.status_code == 200:
            print('用户cookies有效')
            time.sleep(1)
            if '我的首页' in response.text:
                print('通过cookies登录成功')


    def login_cookies_main(self):
        print('用户开始刷新主页!!')
        d = self.get_cooikes()
        print('读取用户cookies!!')
        self.login_with_cookies(d)
        print('通过cookies访问主页!!')



if __name__ == '__main__':
    username = '908099665' # 新浪微博用户
    pwd = '111111' # 新浪微博用户密码
    wb = WeiboCooikesLogin(username, pwd)
    wb.get_cookies_main()   # 得到cookies
    wb.login_cookies_main()  # 用cookies访问主页

输出结果

 

转载于:https://www.cnblogs.com/xiao-apple36/p/8948817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值