前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
项目目标
爬取音效素材
受害者地址
http://sc.chinaz.com/yinxiao/
爬虫代码
导入工具
import requests import parsel请求网站
url = 'http://sc.chinaz.com/yinxiao/index_{}.html'.format(page) 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)解析网站数据,爬取数据
selector = parsel.Selector(response.text) urls = selector.css('#musiclist .n1::attr(thumb)').getall() titles = selector.css('#musiclist .z a::attr(alt)').getall() data = zip(urls, titles) for i in data: print(i) download_url = i[0] title = i[1] response_2 = requests.get(url=download_url, headers=headers)保存数据
filename = 'C:\\Users\\Administrator\\Desktop\\新建文件夹\\' + title + '.mp3' with open(filename, mode='wb') as f: f.write(response_2.content)运行代码,效果如下图