python实现的爬取电影下载链接功能示例


Posted in Python onAugust 26, 2019

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
Python中处理unchecked未捕获异常实例
Jan 17 Python
详细讲解用Python发送SMTP邮件的教程
Apr 29 Python
Python 字典与字符串的互转实例
Jan 13 Python
python3操作mysql数据库的方法
Jun 23 Python
Python自定义简单图轴简单实例
Jan 08 Python
python基于Selenium的web自动化框架
Jul 14 Python
Django框架基础模板标签与filter使用方法详解
Jul 23 Python
Python 中的 import 机制之实现远程导入模块
Oct 29 Python
浅析Django中关于session的使用
Dec 30 Python
pytorch中图像的数据格式实例
Feb 11 Python
Python flask框架实现浏览器点击自定义跳转页面
Jun 04 Python
python实现无边框进度条的实例代码
Dec 30 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
Aug 26 #Python
Python3.6实现根据电影名称(支持电视剧名称),获取下载链接的方法
Aug 26 #Python
Golang GBK转UTF-8的例子
Aug 26 #Python
利用python实现周期财务统计可视化
Aug 25 #Python
Python爬虫运用正则表达式的方法和优缺点
Aug 25 #Python
numpy求平均值的维度设定的例子
Aug 24 #Python
Python实现滑动平均(Moving Average)的例子
Aug 24 #Python
You might like
重置版游戏视频
2020/04/09 魔兽争霸
一个程序下载的管理程序(三)
2006/10/09 PHP
Windows下安装Memcached的步骤说明
2010/04/25 PHP
mysql 查询指定日期时间内sql语句实现原理与代码
2012/12/16 PHP
Linux环境下搭建php开发环境的操作步骤
2013/06/17 PHP
支持中文的PHP按字符串长度分割成数组代码
2015/05/17 PHP
PHPExcel中文帮助手册|PHPExcel使用方法(分享)
2017/06/09 PHP
javascript contains和compareDocumentPosition 方法来确定是否HTML节点间的关系
2010/02/04 Javascript
基于jquery的动态创建表格的插件
2011/04/05 Javascript
分享8款优秀的 jQuery 加载动画和进度条插件
2012/10/24 Javascript
JS中for循序中延迟加载动态效果的具体实现
2013/08/18 Javascript
javascript的回调函数应用示例
2014/02/20 Javascript
jquery的clone方法应用于textarea和select的bug修复
2014/06/26 Javascript
一个实用的图片切换支持点击切换和自动轮播
2014/09/09 Javascript
简介JavaScript中substring()方法的使用
2015/06/06 Javascript
javascript实现动态表头及表列的展现方法
2015/07/14 Javascript
JavaScript之WebSocket技术详解
2016/11/18 Javascript
Bootstrap缩略图与警告框学习使用
2017/02/08 Javascript
Bootstrap响应式表格详解
2017/05/23 Javascript
vue 组件使用中的一些细节点
2018/04/25 Javascript
webpack 代码分离优化快速指北
2019/05/18 Javascript
Vue数字输入框组件使用方法详解
2020/02/10 Javascript
python 查找文件夹下所有文件 实现代码
2009/07/01 Python
Python编程中time模块的一些关键用法解析
2016/01/19 Python
python分析作业提交情况
2017/11/22 Python
Win10环境中如何实现python2和python3并存
2020/07/20 Python
css3实例教程 一款纯css3实现的环形导航菜单
2014/10/20 HTML / CSS
欧铁通票官方在线销售网站:Eurail.com
2017/10/14 全球购物
美国折衷生活方式品牌:Robert Graham
2018/07/13 全球购物
可口可乐唇膏:Lip Smackers
2019/08/27 全球购物
美国购买体育、音乐会和剧院门票网站:SelectATicket
2019/09/08 全球购物
两则小学生的自我评价分享
2013/11/14 职场文书
外贸会计专业自荐信
2014/06/22 职场文书
我的生日感言
2015/08/03 职场文书
Python requests库参数提交的注意事项总结
2021/03/29 Python
25张裸眼3D图片,带你重温童年的记忆,感受3D的魅力
2022/02/06 杂记