日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python實現的爬取電影下載鏈接功能示例

系統 2146 0

本文實例講述了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程序設計有所幫助。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 金昌市| 莲花县| 长泰县| 娄烦县| 双牌县| 安阳市| 富裕县| 新河县| 哈密市| 枝江市| 清丰县| 乡宁县| 台江县| 萝北县| 鄯善县| 海林市| 孝昌县| 阿克苏市| 濮阳市| 临沧市| 富川| 高邮市| 鹰潭市| 彝良县| 万荣县| 安吉县| 宣城市| 黄山市| 登封市| 吉安县| 平邑县| 肇州县| 正镶白旗| 清苑县| 梧州市| 信阳市| 保德县| 永川市| 云和县| 米易县| 辽宁省|