本文的文字及图片来源于网络,仅供学习、交流使用,不具备任何商业用途,若有问题请及时联系咱们以做处理。python
Python爬虫入门教程08:爬取csdn文章保存成PDFui
PS:若有须要 Python学习资料
以及 解答
的小伙伴能够加点击下方连接自行获取
python免费学习资料以及群交流解答点击便可加入
import os import requests
安装Python并添加到环境变量,pip安装须要的相关模块便可。
爬取搞笑趣味栏目的视频内容。
首先须要明确一点,好看视频网站加载方式是懒加载的方式,须要你下滑网页才会加载出新的内容
加载出来的内容里面有音频播放地址以及标题。
内容比较简单,看代码就行
import os import requests url = 'https://haokan.baidu.com/videoui/api/videorec?tab=gaoxiao&act=pcFeed&pd=pc&num=20&shuaxin_id=1612592171486' 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=url, headers=headers) json_data = response.json() videos = json_data['data']['response']['videos'] for index in videos: title = index['title'] play_url = index['play_url'] video_content = requests.get(url=play_url, headers=headers).content path = 'video\\' if not os.path.exists(path): os.mkdir(path) with open(path + title + '.mp4', mode='wb') as f: f.write(video_content) print('正在保存:', title)