python實(shí)現(xiàn)的爬取電影下載鏈接功能示例
本文實(shí)例講述了python實(shí)現(xiàn)的爬取電影下載鏈接功能。分享給大家供大家參考,具體如下:
#!/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頁(yè)面中獲取視頻下載地址
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頁(yè)面中獲取電影標(biāo)題
def get_movie_title(html):
soup=BeautifulSoup(html,'html.parser')
fixed_html=soup.prettify()
title=soup.find('h1')
title=title.string
return title
#訪問(wèn)url,返回html頁(yè)面
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
#從電影列表頁(yè),獲取電影的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")
#傳入頁(yè)數(shù),返回這幾頁(yè)的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 #打算爬取幾頁(yè)電影
p_url = get_pages_url(pages)
for i in p_url:
write_to_txt(get_movie_list(i))#執(zhí)行寫入
print "done"
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python中functools模塊的常用函數(shù)解析
這篇文章主要介紹了Python中functools模塊的常用函數(shù)解析,分別講解了partial、update_wrapper、wraps、total_ordering的用法,需要的朋友可以參考下2016-06-06
Python運(yùn)行錯(cuò)誤異常代碼含義對(duì)照表
這篇文章主要介紹了Python運(yùn)行錯(cuò)誤異常代碼含義對(duì)照表,需要的朋友可以參考下2021-04-04
對(duì)python特殊函數(shù) __call__()的使用詳解
今天小編就為大家分享一篇對(duì)python特殊函數(shù) __call__()的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Python實(shí)現(xiàn)批量繪制遙感影像數(shù)據(jù)的直方圖
這篇文章主要為大家詳細(xì)介紹了如何基于Python中g(shù)dal模塊,實(shí)現(xiàn)對(duì)大量柵格圖像批量繪制直方圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02
matplotlib畫圖之修改坐標(biāo)軸刻度問(wèn)題
這篇文章主要介紹了matplotlib畫圖之修改坐標(biāo)軸刻度問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

