本文实例讲述了python实现的爬取电影下载链接功能。分享给大家供大家参考,具体如下:
#!/usr/bin/python
#coding=UTF-8
import sys
import urllib2
import os
import chardet
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding("utf-8")
#从电影html页面中获取视频下载地址
def get_movie_download_url(html):
soup=BeautifulSoup(html,'html.parser')
fixed_html=soup.prettify()
td=soup.find('td',attrs={'style':'WORD-WRAP: break-word'})
url_a=td.find('a')
url_a=url_a.string
return url_a
#从电影html页面中获取电影标题
def get_movie_title(html):
soup=BeautifulSoup(html,'html.parser')
fixed_html=soup.prettify()
title=soup.find('h1')
title=title.string
return title
#访问url,返回html页面
def get_html(url):
req=urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0')
response=urllib2.urlopen(url)
html=response.read()
return html
#从电影列表页,获取电影的url,拼接好,存到列表后返回
def get_movie_list(url):
m_list = []
html = get_html(url)
soup=BeautifulSoup(html,'html.parser')
fixed_html=soup.prettify()
a_urls=soup.find_all('a',attrs={'class':'ulink'})
host = "http://www.ygdy8.net"
for a_url in a_urls:
m_url=a_url.get('href')
m_list.append(host+m_url)
return m_list
#存入txt文件
def file_edit(wr_str):
f1 = open(r'e:\down_load_url.txt','a')
f1.write(wr_str)
f1.close()
#传入电影url的列表集合,获取下载地址,并写入文件
def write_to_txt(a_urls):
for a_url in a_urls:
html=get_html(a_url)
html=html.decode('GBK')
write_title=get_movie_title(html)
write_url=get_movie_download_url(html)
file_edit(write_title+"\n")
file_edit(write_url+"\n")
file_edit("\n")
#传入页数,返回这几页的url列表
def get_pages_url(num):
urls_list = []
url="http://www.ygdy8.net/html/gndy/dyzz/list_23_"
for n in range(1,num+1):
new_url = url+str(n)+".html"
urls_list.append(new_url)
return urls_list
if __name__=='__main__':
pages = 2 #打算爬取几页电影
p_url = get_pages_url(pages)
for i in p_url:
write_to_txt(get_movie_list(i))#执行写入
print "done"
更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python实现的爬取电影下载链接功能示例”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2025年10月25日
2025年10月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]