码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 2-爬虫-代理池搭建、代理池使用(搭建django后端测试)、爬取某视频网站、爬取某视频网站、bs4介绍和遍历文档树


    1 代理池搭建
    2 代理池使用
    2.1 搭建django后端测试

    3 爬取某视频网站
    4爬取某视频网站
    5 bs4介绍和遍历文档树

    1 代理池搭建

    # ip代理
    	-每个设备都会有自己的IP地址
        -电脑有ip地址---》访问一个网站---》访问太频繁---》封ip
        
        -收费:靠谱稳定--提供api
        -免费:不稳定--自己写api用
        	-开源的:https://github.com/jhao104/proxy_pool
        		免费代理---》爬取免费代理---》验证---》存到redis中
                flask搭建web---》访问某个接口,随机获取ip
                
                
        
    # 搭建步骤:
    	1 git clone git@github.com:jhao104/proxy_pool.git
        2 pycharm中打开
        3 安装依赖:创建虚拟环境  pip install -r requirements.txt
        4 修改配置文件: DB_CONN = 'redis://127.0.0.1:6379/0'
        5 运行调度程序和web程序
            # 启动调度程序
            python proxyPool.py schedule
    
            # 启动webApi服务
            python proxyPool.py server
    
       6 api介绍
        /	GET	api介绍	None
        /get	GET	随机获取一个代理	可选参数: ?type=https 过滤支持https的代理
        /pop	GET	获取并删除一个代理	可选参数: ?type=https 过滤支持https的代理
        /all	GET	获取所有代理	可选参数: ?type=https 过滤支持https的代理
        /count	GET	查看代理数量	None
        /delete	GET	删除代理	?proxy=host:ip
            
            
     # http和https代理
    	-以后使用http代理访问http的地址
        -使用https的代理访问https的地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    2 代理池使用

    "公网" 和 "内网" 是网络术语,用于描述不同的网络范围和可访问性。以下是它们的定义和示例:
    
    **公网 (Internet)**:
    	- **定义:** 公网是指全球范围的互联网,连接了世界各地的计算机、服务器和设备,
    		允许它们通过因特网协议(IP)进行通信。
    	- **示例:** 
    	  - 当您使用浏览器访问网站,例如 Google、Facebook 或 Twitter,
    		您是通过公网与这些网站的服务器通信。
    	  - 电子邮件发送和接收也是通过公网进行的,例如使用 Gmail 或 Outlook 邮箱。
    	  - 在社交媒体上与全球范围内的朋友互动,如发布推文、分享照片或发布视频。
    
    **内网 (Intranet)**:
    	- **定义:** 内网是指一个私有网络,通常在组织、公司或机构内部使用,
    		用于内部通信、数据共享和资源管理。它通常不直接连接到公网。
    	- **示例:** 
    	  - 企业内部网络:大多数组织都有内部网络,用于员工之间的通信和共享内部资源。
    	 	这些网络可以包括内部网站、文件共享和内部电子邮件系统。
    	  - 家庭网络:在家庭网络中,您可以有多个设备(例如台式电脑、笔记本电脑、智能手机、
    	  	智能家居设备等)连接到一个本地路由器,形成一个内部网络。
    	  	这个内部网络允许这些设备共享文件、打印机和互联网连接,但通常不会直接暴露给公网。
    
    在这两个示例中,公网是全球范围的互联网,而内网是限定在特定组织或家庭的私有网络。
    内网通常需要特定的访问权限才能连接到公网,并且通常通过防火墙或路由器进行保护,以确保安全性和隐私。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    2.1 搭建django后端测试

    import requests
    res = requests.get('http://192.168.1.252:5010/get/?type=http').json()['proxy']
    proxies = {
        'http': res,
    }
    print(proxies)
    # 我们是http 要使用http的代理
    respone = requests.get('http://139.155.203.196:8080/', proxies=proxies)
    print(respone.text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ds-1698993940761)

    # 步骤:
    	1 写个django,只要访问,就返回访问者ip
        2 部署在公网上---》python manage.py runserver 0.0.0.0:8000
        3 本机使用代理测试
        import requests
        res1 = requests.get('http://192.168.1.63:5010/get/?type=http').json()
        dic = {'http': res1['proxy']}
        print(dic)
        res = requests.get('http://47.93.190.59:8000/', proxies=dic)
        print(res.text)
        
        
        
        
    # 补充:
    	代理有 透明和高匿
        透明的意思:使用者最终的ip是能看到的
        高匿:隐藏访问者真实ip,服务端看不到
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3 爬取某视频网站

    # 目标:
    	爬取该网站的视频,保存到本地  https://www.pearvideo.com/ 
            
    import requests
    import re
    
    # 请求地址是:
    # https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0
    res = requests.get('https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=1&start=0')
    # print(res.text)
    # 解析出视频地址---》正则
    video_list = re.findall('', res.text)
    # print(video_list)
    for video in video_list:
        video_id = video.split('_')[-1]
        url = 'https://www.pearvideo.com/' + video
        print(url)  # 向视频详情发送请求---》解析出页面中mp4视频地址---》直接下载即可
        header = {
            'Referer': url
        }
        res_json = requests.get(f'https://www.pearvideo.com/videoStatus.jsp?contId={video_id}&mrd=0.14435938848299434',
                                headers=header).json()
        mp4_url = res_json['videoInfo']['videos']['srcUrl']
        real_mp4_url = mp4_url.replace(mp4_url.split('/')[-1].split('-')[0], 'cont-%s' % video_id)
        print(real_mp4_url)
        # 把视频保存到本地
        res_video = requests.get(real_mp4_url)
        with open('./video/%s.mp4' % video_id, 'wb') as f:
            for line in res_video.iter_content(1024):
                f.write(line)
    
    # res=requests.get('https://www.pearvideo.com/video_1526860')
    # print(res.text)
    
    # 第一层反扒:需要携带referfer
    # header = {'Referer': 'https://www.pearvideo.com/video_1527879'}
    # res = requests.get('https://www.pearvideo.com/videoStatus.jsp?contId=1527879&mrd=0.14435938848299434', headers=header)
    # print(res.text)
    
    
    # 反扒二:
    # https://video.pearvideo.com/mp4/adshort/20190311/   1698982998222  -13675354_adpkg-ad_hd.mp4  返回的
    # https://video.pearvideo.com/mp4/adshort/20190311/   cont-1527879   -13675354_adpkg-ad_hd.mp4   能播的
    # s = 'https://video.pearvideo.com/mp4/adshort/20190311/1698982998222-13675354_adpkg-ad_hd.mp4'
    # print(s.replace(s.split('/')[-1].split('-')[0], 'cont-1527879'))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    4 爬取新闻

    # 没有一个解析库---》用正则---》解析库--》html/xml
    import requests
    # pip install BeautifulSoup4
    from bs4 import BeautifulSoup
    
    res = requests.get('https://www.autohome.com.cn/news/1/#liststart')
    # print(res.text)
    # 找到页面中所有的类名叫article  ul标签
    soup = BeautifulSoup(res.text, 'html.parser')
    # bs4的查找
    ul_list = soup.find_all(class_='article', name='ul')  # 所有的类名叫article  ul标签
    print(len(ul_list))
    # 循环再去没一个中,找出所有li
    for ul in ul_list:
        li_list = ul.find_all(name='li')
        for li in li_list:
            h3 = li.find(name='h3')
            if h3:
                title = h3.text
                url = 'https:' + li.find(name='a')['href']
                if url.startswith('//'):
                    url = 'https:' + url
                desc = li.find(name='p').text
                reade_count = li.find(name='em').text
                img = li.find(name='img')['src']
    
                print(f'''
                文章标题:{title}
                文章地址:{url}
                文章摘要:{desc}
                文章阅读数:{reade_count}
                文章图片:{img}
                ''')
    
    
    # 爬5页--->把图片保存到本地--->把打印的数据存储到mysql中--》建个表
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    5 bs4介绍和遍历文档树

    # BeautifulSoup 是一个可以从HTML或XML文件中提取数据的Python库,解析库
    # pip install beautifulsoup4
    
    • 1
    • 2
    from bs4 import BeautifulSoup
    
    html_doc = """
    The Dormouse's story
    
    

    The Dormouse's storylqz

    Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

    ...

    """
    soup = BeautifulSoup(html_doc, 'html.parser') # 解析库可以使用 lxml,速度快(必须安装) 可以使用python内置的 html.parser # print(soup.prettify()) -----重点:遍历文档树---------- #遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个 #1、用法 通过 . 遍历 # res=soup.html.head.title # res=soup.p # print(res) #2、获取标签的名称 # res=soup.html.head.title.name # res=soup.p.name # print(res) #3、获取标签的属性 # res=soup.body.a.attrs # 所有属性放到字典中 :{'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'} # res=soup.body.a.attrs.get('href') # res=soup.body.a.attrs['href'] # res=soup.body.a['href'] # print(res) #4、获取标签的内容 # res=soup.body.a.text #子子孙孙文本内容拼到一起 # res=soup.p.text # res=soup.a.string # 这个标签有且只有文本,才取出来,如果有子孙,就是None # res=soup.p.strings # print(list(res)) #5、嵌套选择 # 下面了解 #6、子节点、子孙节点 # print(soup.p.contents) #p下所有子节点 # print(list(soup.p.children)) #得到一个迭代器,包含p下所有子节点 # print(list(soup.p.descendants)) #获取子子孙节点,p下所有的标签都会选择出来 #7、父节点、祖先节点 # print(soup.a.parent) #获取a标签的父节点 # print(list(soup.a.parents) )#找到a标签所有的祖先节点,父亲的父亲,父亲的父亲的父亲... #8、兄弟节点 # print(soup.a.next_sibling) #下一个兄弟 # print(soup.a.previous_sibling) #上一个兄弟 # print(list(soup.a.next_siblings)) #下面的兄弟们=>生成器对象 # print(soup.a.previous_siblings) #上面的兄弟们=>生成器对象
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    软件工程师进入编程世界的55个锦囊:《 好代码 ,坏代码》
    设计模式-状态模式在Java中的使用示例-信用卡业务系统
    对象业务的追加写接口
    代码 Revert 后再次 Merge 会丢失的问题,已解决
    二手车交易管理系统
    nagios
    全开源、低代码开发平台,可以搭建什么样的企业网盘?
    C++经验(十一)-- (inlining)内联函数的使用
    TCP 小结
    基于HTML+CSS+JavaScript制作响应式个人博客模板源码( JavaScript期末大作业 )
  • 原文地址:https://blog.csdn.net/weixin_44145338/article/details/134203142
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号