序言

文中的文本及图片过虑互联网,能够学习培训 ,沟通交流应用,不具备一切商业行为,如有什么问题请立即在线留言以作解决 。

下列文章内容来自青灯程序编写  ,作者:轻风

 

 

 

如圖所显示 ,抓取171个视頻,总共2.8G的内存空间,用时仅有88秒 ,还不上一分半。

基础开发工具

  • Python 3.6
  • 皮查姆

有关控制模块的应用

import re
import time
import requests
import concurrent.futures

 

有关控制模块pip安装就可以。

 

详细编码

import re
import time
import requests
import concurrent.futures




def get_response(html_url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
    response = requests.get(url=html_url, headers=headers)
    return response




def save(video_url, video_title):
    filename = 'video\\'   video_title   '.mp4'
    video_data = get_response(video_url).content
    with open(filename, mode='wb') as f:
        f.write(video_data)
        print('已经储存:', video_title)




def main(html_url):
    html_data = get_response(html_url).text
    lis = re.findall('<div id="(\d )" class="newslv_share">', html_data)
    for li in lis:
        page_url = f'https://www.thepaper.cn/newsDetail_forward_{li}'
        page_data = get_response(page_url).text
        video_url = re.findall('<source src="(.*?)" type="video/mp4"/>', page_data)[0]
        video_title = re.findall('<h2>(.*?)</h2>', page_data)[0]
        save(video_url, video_title)


    end_time = time.time()
    use_time = end_time - start_time
    print('一共用时:', use_time)




if __name__ == '__main__':
    start_time = time.time()
    executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
    for page in range(1, 11):
        url = f'https://www.thepaper.cn/load_video_chosen.jsp?channelID=26916&pageidx={page}'
        executor.submit(main, url)
    executor.shutdown()

 

详细编码早已给了,自身的体会〜,我这還是应用的五个进程 ,你给10个进程高效率会高些,能够一分钟不上就可以爬完后

Python网络爬虫、数据统计分析 、网站建设等实例视频教程在线免费收看

https://space.bilibili.com/523606542

 

文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归趣快排www.sEoguruBlog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ✈61910465