Loading... ## 唠嗑 ## 这些个网站,广告巨多,看个片,要一直往下滑好久才能看到想要的内容,上边全是bc广告 <img src="https://www.z2blog.com/usr/themes/handsome/assets/img/emotion/aru/distressed.png" class="emotion-aru"> 这怎么能行,爬虫爬下来,只保留视频标题和播放链接和下载链接,看什么一目了然,广告什么的,死一边去 <img src="https://www.z2blog.com/usr/themes/handsome/assets/img/emotion/aru/cheer.png" class="emotion-aru"> ## 动手 ## ### 首页 进入首页往下滑,最下边,可以看到今日更新的内容,我们就只爬取今天更新的,每日一爬 在首页可以采集到,内容页链接和封面图以及标题(吸不吸人标题最重要 <img src="https://www.z2blog.com/usr/themes/handsome/assets/img/emotion/aru/shy.png" class="emotion-aru"> ) ### 内容页 由首页最近更新的地方进入内容页后,可以看到下载链接和以及让我们选择的播放地址 ![Snipaste_2019-12-15_15-42-11.png][1] 于是乎,在内容页,我们可以采集到**资源的下载链接** 下载链接的正则表达式 <a href="https://www.z2blog.com/index.php/go/KC4qKQ==" target="_blank" class="btn btn-sm btn-default hidden-xs c_white" title="迅雷影音" target="_blank"><img src=".*" /></a> 也可以使用Python相关的模块,更方便操作,例如beautifySoup,博主也是新人,这模块安装了,但是没有具体使用过,但是使用这种模块对于数据采集非常的高效。 ### 播放页 点击内容页的在线播放线路,来到播放页,浏览器右键查看网页源代码,我们可以直接找到一段js代码,两个播放线路对应m3u8资源链接 ![Snipaste_2019-12-15_15-48-33.png][2] 所以 播放链接1为 m3u8_host + video 播放链接2为 m3u8_host1 + video 那么正则提取对于数据,在拼接完整播放连接即可 正则表达式: var video[\s]*=[ ]\'(.*)\';[\s]*var m3u8_host[\s]*= \'(.*)\';[\s]*var m3u8_host1[ ]=[ ]\'(.*)\' 最后保存数据就可以了,可以保存为txt文件,本地使用,也可以保存到数据库。 <div class="preview"> <div class="post-inser post box-shadow-wrap-normal"> <a href="https://www.z2blog.com/index.php/learn/100.html" target="_blank" class="post_inser_a no-external-link"> <div class="inner-image bg" style="background-image: url(http://assets.z2blog.com/usr/uploads/2019/11/3571361852.png);background-size: cover;"></div> <div class="inner-content" > <p class="inser-title">Python采集电影资源并存入数据库</p> <div class="inster-summary text-muted"> 这是上次完成的Python实现简单爬取电影天堂资源 初学爬虫,上次将数据采集下来,并且存入字典中。这些数据,需要随... </div> </div> </a> <!-- .inner-content #####--> </div> <!-- .post-inser ####--> </div> 保存数据库了,还需要前台网站显示,不然看片有点麻烦。 Python自学进程快到web框架了,之后就可以用上了。 ## 完整代码 ## ```python import os import time import requests import re import threading HOST = 'https://www.akz96.com/' def total_links(links, img_url, title): s, cate, url = links.split('/') # 拼接播放页 play_page_links = HOST + cate + '/' + 'play-' + url + '?road=1' down_page_links = HOST + links play_link = get_video_links(play_page_links) down_link = get_video_down(down_page_links) save(title, play_link, down_link) def get_video_links(play_page_links): play_page = requests.get(play_page_links) play_page.encoding = 'utf-8' play_data = re.findall(r'var video[\s]*=[ ]\'(.*)\';[\s]*var m3u8_host[\s]*= \'(.*)\';[\s]*var m3u8_host1[ ]=[ ]\'(.*)\'', play_page.text) play_data = play_data[0] play_links_1 = play_data[1] + play_data[0] play_links_2 = play_data[2] + play_data[0] return [play_links_1, play_links_2] def get_video_down(down_page_links): res = requests.get(down_page_links) res.encoding = 'utf8' res = re.findall(r'<a href="https://www.z2blog.com/index.php/go/KC4qKQ==" target="_blank" class="btn btn-sm btn-default hidden-xs c_white" title="迅雷影音" target="_blank"><img src=".*" /></a>', res.text) down_link = res[0] return down_link def save(title, play_link, down_link): with open('./宅男福利爬取结果/{}.txt'.format(title), 'w') as f: f.write('在线播放地址:\r\n') f.write(play_link[0] + '\r\n' + play_link[1]) f.write('\r\n资源下载链接:\r\n') f.write(down_link) def main(): headers = { 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', } index = requests.get(HOST + 'index/home.html', headers) index.encoding = 'utf-8' index_res = re.findall( r'<li class="content-item">[\s]*<a target="_blank" href="https://www.z2blog.com/index.php/go/KC4qKQ==" target="_blank" >[\s]*<div class="content-img lazy" data-original="(.*)" data-prefix=""[\s]*style=".*"></div>[\s]*<div class="content-text">[\s]*<p> (.*)</p>', index.text) os.mkdir('./宅男福利爬取结果') for links, img_url, title in index_res: # 拼接完整内容页连接 t = threading.Thread(target=total_links, args=(links, img_url, title)) t.start() time.sleep(1) if __name__ == '__main__': main() ``` ## 关于下载链接 ## 下载链接可以在迅雷使用,边下边播,大多数朋友应该是手机上看 <img src="https://www.z2blog.com/usr/themes/handsome/assets/img/emotion/aru/proud.png" class="emotion-aru"> 在线观看的链接有些时候会卡,毕竟服务器是在国外,所以推荐使用下载链接观看,更流畅 <div class="preview"> <div class="post-inser post box-shadow-wrap-normal"> <a href="https://www.z2blog.com/index.php/default/269.html" target="_blank" class="post_inser_a no-external-link"> <div class="inner-image bg" style="background-image: url(http://assets.z2blog.com/usr/uploads/2019/12/980866422.png);background-size: cover;"></div> <div class="inner-content" > <p class="inser-title">推荐一款磁力搜索与下载APP,支持边下边播</p> <div class="inster-summary text-muted"> 你是否遇到迅雷敏感资源限制不开迅雷会员,这速度慢的让人心疼最新电影,时常遇见因为版权方要求,不支持下载于是,推荐给... </div> </div> </a> <!-- .inner-content #####--> </div> <!-- .post-inser ####--> </div> [1]: http://assets.z2blog.com/usr/uploads/2019/12/914929375.png [2]: http://assets.z2blog.com/usr/uploads/2019/12/560674567.png Last modification:December 15th, 2019 at 04:19 pm © 允许规范转载