python实现的爬取电影下载链接功能示例
作者:zhizunyu2009 时间:2023-02-06 20:30:47
本文实例讲述了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程序设计有所帮助。
来源:https://blog.csdn.net/zhizunyu2009/article/details/77854655
标签:python,爬取,下载链接
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
block 和 inline 的区别是?
2009-12-08 13:00:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/203330321-54s.gif)
使用TensorFlow实现简单线性回归模型
2022-11-30 19:51:48
![](https://img.aspxhome.com/file/2023/5/92515_0s.jpg)
python 视频下载神器(you-get)的具体使用
2023-03-18 19:30:58
![](https://img.aspxhome.com/file/2023/8/95418_0s.png)
Python中的pygal安装和绘制直方图代码分享
2021-11-18 15:09:50
![](https://img.aspxhome.com/file/2023/9/87479_0s.png)
一实用的table内容排序Javascript类库
2008-11-02 15:03:00
MySQL数据库root权限丢失解决方案
2008-07-13 13:59:00
用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
2023-09-28 11:01:21
![](https://img.aspxhome.com/file/2023/7/90347_0s.png)
python中如何设置list步长
2022-11-04 14:24:02
文字解说Golang Goroutine和线程的区别
2023-10-15 18:56:36
python实现巡检系统(solaris)示例
2022-03-01 20:32:11
javascript preload&lazy load
2023-09-01 06:15:43
Oracle数据库的实例/表空间/用户/表之间关系简单讲解
2023-07-20 11:48:34
![](https://img.aspxhome.com/file/2023/4/64474_0s.jpg)
python设置中文界面实例方法
2023-08-30 18:56:30
![](https://img.aspxhome.com/file/2023/8/62278_0s.png)
教你在SQL Server 2000数据库中使用分区
2008-11-25 11:55:00
![](https://img.aspxhome.com/file/UploadPic/200811/20081130132452228.jpg)
python next()和iter()函数原理解析
2023-04-05 21:58:50
Laravel框架文件上传功能实现方法示例
2023-11-15 19:16:31
![](https://img.aspxhome.com/file/2023/7/89717_0s.png)
ASP去掉字符串头尾连续回车和空格的Function
2011-04-03 11:13:00
自定义数据库自动编号初始值和步进值
2009-05-19 10:00:00
关于asp分页的新想法
2008-09-06 12:53:00
python ImageDraw类实现几何图形的绘制与文字的绘制
2023-10-14 10:58:13
![](https://img.aspxhome.com/file/2023/2/97032_0s.jpg)