zoukankan      html  css  js  c++  java
  • 用爬虫虚拟登陆网页及爬取视频的方法

    今日内容概要

    • requests模块其他方法介绍
    • 利用requests模块模拟网站的登录
    • 利用requests模块爬取梨视频网站的视频资源
    • requests—html模块(是requests模块的进阶版本)
    • IP代理池及如何在模块中使用
    • beautifulsoup模块(能够避免你亲自书写很多正则表达式)

    今日内容详细

    利用requests模块模拟网站的登录

    # 必备知识点
    requests.post(
        url,
        headers,
        data,    # 发送post请求携带的数据(请求体里面的数据)
        params,    # get请求可以携带的数据
     )
    
    # 模拟登录华华手机
    # 发送post请求的地址  http://www.aa7a.cn/user.php
    # 朝该网站发送post请求请求体的数据格式
    """
    爬虫所有的步骤都需要你自己推导猜测
    username: 3189374495@qq.com
    password: Yzl1998
    captcha: EKTH
    remember:1
    ref:http://www.aa7a.cn/user.php&ref=http%3A%2F%2Fwww.aa7a.cn%2Fuser.php%3Fact%3Dlogout
    act: act_login
    """
    import requests
    
    data = {
        'usename':'3189374495@qq.com',
        'password':'Tj1998',
        'captcha':'EKTH',
        'remeber':1,
      'ref':'http://www.aa7a.cn/user.php&ref=http%3A%2F%2Fwww.aa7a.cn%2Fuser.php%3Fact%3Dlogout',
        'act':'act_login'
    }
    headers = {
      'Referer':'http://www.aa7a.cn/user.php&ref=http%3A%2F%2Fwww.aa7a.cn%2Fuser.php%3Fact%3Dlogout'
     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'   
    }
    res = requests.post(
        url='http://www.aa7a.cn/user.php',
        data=data,
        headers=headers
    )
    cookie = res.cookies.get_dict() # 直接获取cookie的字典格式数据
    # print(cookie)
    """
    {'ECS[password]':'b694dc52c4e4ef7de95e2492e49ff0d4',
    'ECS[user_id]':'66853',
    'ECS[username]':'3189374495%40qq.com',
    'ECS[visit_times]':'6',
    'ECS_ID':'0fe834205463549a66b1ad3f7103676f1e0ec20a'}
    """
    # 获取到网站返回的cookie数据之后  直接在访问该网站的时候带着cookie就可以了
    # 访问网站的首页
    ses = requests.get(url='http://aa7a.cn',headers=headers,cookies=cookie)
    
    # print(ses.text)
    if '3189374495@qq.com' in ses1.text:
        print('登录成功')
    else:
        print('登录失败')
    """
    在使用爬虫程序进行登录操作的时候基本步骤如下
         1.首先明确该网站的登录地址
         2.发送的时候先用浏览器查看请求体数据格式
         3.发送post请求获取网站返回的cookie信息(*****************************)
         4.拿着cookie去访问该网站的其他页面(已登录的身份访问进去的)
              cookie = res.cookies.get_dict()
    """    
    

    爬取梨视频

    1.导航条每个栏目后缀不一样
              https://www.pearvideo.com/
    2.加载更多的数据地址
    	https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=36&mrd=0.26149903889753423&filterIds=1697273,1697301,1697159,1697272,1697283,1697286,1697280,1697278,1697258,1697234,1697269,1697248,1697263,1697257,1697198
        直接拿到数据的链接地址和简介
    3.分析得知 社会栏目所有页的视频数据加载规律
    	https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0
        由start后面的数字来控制
    4.研究视频的地址
    	<a href="video_1697355" class="vervideo-lilink actplay"></a>
        # 获取到href之后还需要你自己手动拼接前面缺失的地址
        https://www.pearvideo.com/
    5.朝上述地址发送get请求获取数据
    	
        
    # 具体代码
    """
    1.导航条每个栏目后缀不一样
    	https://www.pearvideo.com/category_N
    2.加载更多的数据地址
    	https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=36&mrd=0.26149903889753423&filterIds=1697273,1697301,1697159,1697272,1697283,1697286,1697280,1697278,1697258,1697234,1697269,1697248,1697263,1697257,1697198
    直接拿到数据的链接地址和简介
    3.分析得知 社会栏目所有页的视频数据加载规律
    	https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0
        由start后面的数字来控制
    4.研究视频的地址
    	<a href="video_1697355" class="vervideo-lilink actplay"></a>
        # 获取到href之后还需要你自己手动拼接前面缺失的地址
        https://www.pearvideo.com/
    5.朝上述地址发送get请求获取数据
    """
    import requests
    import re
    res = requests.get(url='https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0')
    # print(res.text,type(res.text))  # 获取到页面资源
    # 从上述字符串中获取a标签里面的href里面的视频地址后缀	
    # <a href="video_1697319" class="vervideo-lilink actplay">
    reg_exp = '<a href="(.*?)" class="vervideo-lilink actplay">'
    res1 = re.findall(reg_exp,res.text)
    # print(res1)  # ['video_1697362', 'video_1697355', 'video_1697381', 'video_1697378', 'video_1697341', 'video_1697336', 'video_1697329', 'video_1697319', 'video_1697318', 'video_1697105', 'video_1697316', 'video_1697312']
    # 拼接视频的完整路径
    for i in res1:
        # 拼接出来的地址是一个网页地址 我们要的视频在这个网页里面
        url = 'https://www.pearvideo.com/' + i
        # 还要去分析网页里面视频的真正地址
        '''进一步分析得到网页上面的视频链接地址并不是直接写在html页面上的
        而是通过js代码动态加载的 也就意味着针对当前页面我们无法直接通过正则匹配到
        所以我们需要取研究js代码中获取视频的链接地址
        '''
        # 不再书写匹配html代码的正则而是书写匹配js代码中视频的链接地址正则
        # srcUrl="https://video.pearvideo.com/mp4/adshort/20200916/cont-1697362-15384591_adpkg-ad_hd.mp4"
        wait_reg = 'srcUrl="(.*?)"'
        ret = requests.get(url=url)
        res2 = re.findall(wait_reg,ret.text)
        path = res2[0]
        # print(res2[0])  # https://video.pearvideo.com/mp4/adshort/20200916/cont-1697377-15384776_adpkg-ad_hd.mp4
        # 朝上述地址发送get请求获取视频资源 然后利用文件操作保存到本地
        res3 = requests.get(url=path)
        # 文件名直接用视频地址的最后结尾
         res4 = path.rsplit('/',maxsplit=1)
        # 取列表索引1的元素
        file_name = res4[1]
        print(file_name)
        # 这里的file_name是一个相对路径  如果你想保存到特定的路径下 你需要再次做路径拼接
        root_path = 'D:梨视频存放目录'
        # 拼接路径 千万不要手动拼接 因为不同的操作系统路径分隔符不一样
        import os
        file_path = os.path.join(root_path,file_name)
        print(file_path)
        with open(file_path,'wb') as f:
            # 一行行读取视频数据写入文件
            for line in res3.iter_content():  # 获取二进制流数据
                f.write(line)
    

    总结

    如何获取网页的二进制数据
    res = requests.get(url='https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0')
    print(res.content)
    
    如何获取二进制流数据(一行行读取)
    res3 = requests.get(url='https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0')
    for line in res3.iter_content():  # 获取二进制流数据
        f.write(line)
    

    requests-html模块

    参考文档
    http://requests-html.kennethreitz.org/
    """
    就是照着文档上面的代码 敲一遍 
    运行感受代码产生的功能
    """
    

    补充

    ***************************************
    有时候浏览器获取到的html文件并不是一个完整的文件
    上面的HTML标签有一些是直接写死的 直接就可以查看到
    而有一些是通过后续的js代码动态加载出来的 你是查看不到的
    ***************************************
    
    为了舒适的结果,眼前的坎坷路程即使再长都是值得的。
  • 相关阅读:
    配置PyDev,开始eclipsePython之旅
    PyDev下PyQt 的尝试
    逻辑回归 C++
    HP Unix vsftp服务配置
    线性回归(最小二乘法、批量梯度下降法、随机梯度下降法、局部加权线性回归) C++
    批量梯度下降(Batch gradient descent) C++
    利用expect验证主机口令
    python Paramiko 模块远程管理主机
    文件系统巡检
    awk查找特定字段
  • 原文地址:https://www.cnblogs.com/abudrSatan1998/p/13682309.html
Copyright © 2011-2022 走看看