• 一、基础设计


    实验环境 Python3+jupyter
    一、程序框架
    图片.png
    二、工具层 utils.py
    1. 单例函数

    def singleton(cls):
        instance = cls()
        instance.__call__ = lambda: instance
        return instance
    

    三、网络层 jquery.py
    要求:
    1. 实现get、post请求
    2. 添加cookie和headers
    3. 处理http状态码异常
    4. 判断返回数据是json 还是str
    5. 每个用户的cookies独立
    6. 实现cookie文件和cookie的转化

    代码:

    import json,requests
    import  http.cookiejar as cookielib
    from utils import singleton
    
    class session(requests.Session):
        def __init__(self,headers={},cookie_file=None):
            requests.Session.__init__(self)
            self.cookies = cookielib.LWPCookieJar(filename=cookie_file)
            try:self.cookies.load(ignore_discard=True)
            except:print("failed load cookie_file")
            self.headers=headers
            self.auth = ('user', 'pass')
            self._type='str'
            
        def save(self):
            self.cookies.save()
    @singleton
    class http():
        def get(self,s,url,data=None):
            r=s.get(url,params=data)
            self.error(r.status_code)
            return self.res_data(r,s._type)
            
        def post(self,s,url,data):
            r=s.post(url,data=data)
            self.error(r.status_code)
            return self.res_data(r,s._type)
            
        def error(self,code):
            if code==200:return
            elif code==403:raise RuntimeError('403 - Forbidden')
            elif code==404:raise RuntimeError('404 -  Not Found')
            
        def res_data(self,r,_type):
            if _type=='json':return json.loads(r.content)
            return r.content
    

    四、代码测试

    headers={
        'Host': 'api.live.bilibili.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate, br',
        'Referer': 'https://live.bilibili.com/',
        'Origin': 'https://live.bilibili.com',
        'Connection': 'keep-alive'
        }
    s=session(headers,'cookie.txt')
    s._type='json'
    url='https://api.live.bilibili.com/room/v1/Room/playUrl'
    data={
        'cid':7603080,
        'quality':0,
        'platform': 'web'
    }
    r=http.get(s,url,data)
    for d in r['data']['durl']:print(d['url'])
    
    failedload cookie_file
    [https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562](https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562)
    [https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562](https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562)
    [https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562](https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562)
    [https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562](https://qn.live-play.acgvideo.com/live-qn/165966/live_267605959_3403844.flv?wsSecret=06028657c2cc08ff816df9d9d505a5a3&wsTime=1538217562)
    
    
  • 相关阅读:
    &【12】Python 函数
    &【11】Python 流程控制
    &【09】Python 数据类型之dictionary
    &【07】Python 数据类型之元组
    &【08】Python 数据类型之set
    &【06】Python 数据类型之list
    &【05】Python 彻底搞懂分片操作
    &【04】Python 数据结构之序列
    SpringBoot-HelloWorld(三)
    SpringBoot-了解微服务(二)
  • 原文地址:https://www.cnblogs.com/blogs-qq2685054765/p/9727437.html
Copyright © 2020-2023  润新知