欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于python實現(xiàn)的抓取騰訊視頻所有電影的爬蟲

 更新時間:2016年04月22日 20:35:02   投稿:mdxy-dxy  
這篇文章主要介紹了用python實現(xiàn)的抓取騰訊視頻所有電影的爬蟲,這個程序使用芒果存, 所以大家需要下載使用mongodb才可以

我搜集了國內(nèi)10幾個電影網(wǎng)站的數(shù)據(jù),里面近幾十W條記錄,用文本沒法存,mongodb學(xué)習(xí)成本非常低,安裝、下載、運行起來不會花你5分鐘時間。

# -*- coding: utf-8 -*-
# by awakenjoys. my site: www.dianying.at
import re
import urllib2
from bs4 import BeautifulSoup
import string, time
import pymongo
 
NUM  = 0   #全局變量,電影數(shù)量
m_type = u''  #全局變量,電影類型
m_site = u'qq' #全局變量,電影網(wǎng)站
 
#根據(jù)指定的URL獲取網(wǎng)頁內(nèi)容
def gethtml(url):
 req = urllib2.Request(url) 
 response = urllib2.urlopen(req) 
 html = response.read()
 return html
 
#從電影分類列表頁面獲取電影分類
def gettags(html):
 global m_type
 soup = BeautifulSoup(html)  #過濾出分類內(nèi)容
 #print soup
 #<ul class="clearfix _group" gname="mi_type" gtype="1">
 tags_all = soup.find_all('ul', {'class' : 'clearfix _group' , 'gname' : 'mi_type'})
 #print len(tags_all), tags_all
 #print str(tags_all[1]).replace('\n', '')
 
 #<a _hot="tag.sub" class="_gtag _hotkey"  title="動作" tvalue="0">動作</a>
 re_tags = r'<a _hot=\"tag\.sub\" class=\"_gtag _hotkey\" href=\"(.+?)\" title=\"(.+?)\" tvalue=\"(.+?)\">.+?</a>'
 p = re.compile(re_tags, re.DOTALL)
 
 tags = p.findall(str(tags_all[0]))
 if tags:
  tags_url = {}
  #print tags
  for tag in tags:
   tag_url = tag[0].decode('utf-8')
   #print tag_url
   m_type = tag[1].decode('utf-8')
   tags_url[m_type] = tag_url 
    
 else:
   print "Not Find"
 return tags_url
 
#獲取每個分類的頁數(shù)
def get_pages(tag_url):
 tag_html = gethtml(tag_url)
 #div class="paginator
 soup = BeautifulSoup(tag_html)  #過濾出標記頁面的html
 #print soup
 #<div class="mod_pagenav" id="pager">
 div_page = soup.find_all('div', {'class' : 'mod_pagenav', 'id' : 'pager'})
 #print div_page #len(div_page), div_page[0]
 
 #<a class="c_txt6"  title="25"><span>25</span></a>
 re_pages = r'<a class=.+?><span>(.+?)</span></a>'
 p = re.compile(re_pages, re.DOTALL)
 pages = p.findall(str(div_page[0]))
 #print pages
 if len(pages) > 1:
  return pages[-2]
 else:
  return 1
  
 
def getmovielist(html):
 soup = BeautifulSoup(html)
 
 #<ul class="mod_list_pic_130">
 divs = soup.find_all('ul', {'class' : 'mod_list_pic_130'})
 #print divs
 for div_html in divs:
  div_html = str(div_html).replace('\n', '')
  #print div_html
  getmovie(div_html)
 
 
def getmovie(html):
 global NUM
 global m_type
 global m_site
 
 #<h6 class="caption"> <a  target="_blank" title="徒步旅行隊">徒步旅行隊</a> </h6> <ul class="info"> <li class="desc">法國賣座喜劇片</li> <li class="cast"> </li> </ul> </div> <div class="ext ext_last"> <div class="ext_txt"> <h3 class="ext_title">徒步旅行隊</h3> <div class="ext_info"> <span class="ext_area">地區(qū): 法國</span> <span class="ext_cast">導(dǎo)演: </span> <span class="ext_date">年代: 2009</span> <span class="ext_type">類型: 喜劇</span> </div> <p class="ext_intro">理查德·達奇擁有一家小的旅游公司,主要經(jīng)營法國游客到非洲大草原的旅游服務(wù)。六個法國游客決定參加理查德·達奇組織的到非洲的一...</p>
 
 re_movie = r'<li><a class=\"mod_poster_130\" href=\"(.+?)\" target=\"_blank\" title=\"(.+?)\"><img.+?</li>'
 p = re.compile(re_movie, re.DOTALL)
 movies = p.findall(html)
 if movies:
  conn = pymongo.Connection('localhost', 27017)
  movie_db = conn.dianying
  playlinks = movie_db.playlinks
  #print movies
  for movie in movies:
   #print movie
   NUM += 1
   print "%s : %d" % ("=" * 70, NUM)
   values = dict(
    movie_title = movie[1],
    movie_url = movie[0],
    movie_site  = m_site,
    movie_type  = m_type
    )
   print values
   playlinks.insert(values)
   print "_" * 70
   NUM += 1
   print "%s : %d" % ("=" * 70, NUM)
 
 #else:
 # print "Not Find"
 
def getmovieinfo(url):
 html = gethtml(url)
 soup = BeautifulSoup(html)
 
 #pack pack_album album_cover
 divs = soup.find_all('div', {'class' : 'pack pack_album album_cover'})
 #print divs[0]
 
 #<a  target="new" title="《血滴子》獨家紀錄片" wl="1"> </a> 
 re_info = r'<a href=\"(.+?)\" target=\"new\" title=\"(.+?)\" wl=\".+?\"> </a>'
 p_info = re.compile(re_info, re.DOTALL)
 m_info = p_info.findall(str(divs[0]))
 if m_info:
  return m_info
 else:
  print "Not find movie info"
 
 return m_info
 
 
def insertdb(movieinfo):
 global conn
 movie_db = conn.dianying_at
 movies = movie_db.movies
 movies.insert(movieinfo)
 
if __name__ == "__main__":
 global conn
 
 tags_url = "http://v.qq.com/list/1_-1_-1_-1_1_0_0_20_0_-1_0.html"
 #print tags_url
 tags_html = gethtml(tags_url)
 #print tags_html
 tag_urls = gettags(tags_html)
 #print tag_urls
 
 
 for url in tag_urls.items():
  print str(url[1]).encode('utf-8') #,url[0]
  maxpage = int(get_pages(str(url[1]).encode('utf-8')))
  print maxpage
 
  for x in range(0, maxpage):
   #http://v.qq.com/list/1_0_-1_-1_1_0_0_20_0_-1_0.html
   m_url = str(url[1]).replace('0_20_0_-1_0.html', '')
   movie_url = "%s%d_20_0_-1_0.html" % (m_url, x)
   print movie_url
   movie_html = gethtml(movie_url.encode('utf-8'))
   #print movie_html
   getmovielist(movie_html)
   time.sleep(0.1)

相關(guān)文章

  • 使用scrapy ImagesPipeline爬取圖片資源的示例代碼

    使用scrapy ImagesPipeline爬取圖片資源的示例代碼

    這篇文章主要介紹了使用scrapy ImagesPipeline爬取圖片資源的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Pytorch中torch.cat()函數(shù)的使用及說明

    Pytorch中torch.cat()函數(shù)的使用及說明

    這篇文章主要介紹了Pytorch中torch.cat()函數(shù)的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • python中有關(guān)時間日期格式轉(zhuǎn)換問題

    python中有關(guān)時間日期格式轉(zhuǎn)換問題

    這篇文章主要介紹了python中有關(guān)時間日期格式轉(zhuǎn)換問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Python搭建Keras CNN模型破解網(wǎng)站驗證碼的實現(xiàn)

    Python搭建Keras CNN模型破解網(wǎng)站驗證碼的實現(xiàn)

    這篇文章主要介紹了Python搭建Keras CNN模型破解網(wǎng)站驗證碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • python向json中追加數(shù)據(jù)的兩種方法總結(jié)

    python向json中追加數(shù)據(jù)的兩種方法總結(jié)

    JSON用來存儲和交換文本信息,比xml更小/更快/更易解析,下面這篇文章主要給大家介紹了關(guān)于python向json中追加數(shù)據(jù)的兩種方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • 深入淺析Python 函數(shù)注解與匿名函數(shù)

    深入淺析Python 函數(shù)注解與匿名函數(shù)

    這篇文章主要介紹了Python 函數(shù)注解與匿名函數(shù)的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • python三大神器之fabric使用教程

    python三大神器之fabric使用教程

    fabric 是一個python包 是一個基于ssh的部署工具包,這篇文章主要介紹了python三大神器之fabric,需要的朋友可以參考下
    2019-06-06
  • Python判斷兩個對象相等的原理

    Python判斷兩個對象相等的原理

    這篇文章主要介紹了Python判斷兩個對象相等的原理,需要的朋友可以參考下
    2017-12-12
  • Python 在字符串中加入變量的實例講解

    Python 在字符串中加入變量的實例講解

    下面小編就為大家分享一篇Python 在字符串中加入變量的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 教你利用python實現(xiàn)企業(yè)微信發(fā)送消息

    教你利用python實現(xiàn)企業(yè)微信發(fā)送消息

    今天帶大家來練習(xí)python實戰(zhàn),文中對利用python實現(xiàn)企業(yè)微信發(fā)送消息作了詳細的圖文解說及代碼示例,對正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05

最新評論