使用python實(shí)現(xiàn)抓取騰訊視頻所有電影的爬蟲
用python實(shí)現(xiàn)的抓取騰訊視頻所有電影的爬蟲
# -*- coding: utf-8 -*-
import re
import urllib2
from bs4import 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
#<ulclass="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="動(dòng)作" tvalue="0">動(dòng)作</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 tagin 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
#獲取每個(gè)分類的頁數(shù)
def get_pages(tag_url):
tag_html = gethtml(tag_url)
#divclass="paginator
soup = BeautifulSoup(tag_html) #過濾出標(biāo)記頁面的html
#print soup
#<divclass="mod_pagenav" id="pager">
div_page = soup.find_all('div', {'class' :'mod_pagenav','id' :'pager'})
#print div_page #len(div_page), div_page[0]
#<aclass="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)
#<ulclass="mod_list_pic_130">
divs = soup.find_all('ul', {'class' :'mod_list_pic_130'})
#print divs
for div_htmlin 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
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 moviein 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="《血滴子》獨(dú)家紀(jì)錄片" 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 urlin 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 xin 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)
總結(jié)
以上所述是小編給大家介紹的使用python實(shí)現(xiàn)抓取騰訊視頻所有電影的爬蟲,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
一個(gè)基于flask的web應(yīng)用誕生 組織結(jié)構(gòu)調(diào)整(7)
一個(gè)基于flask的web應(yīng)用誕生第七篇,這篇文章主要介紹了組織結(jié)構(gòu)的調(diào)整,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Python face_recognition實(shí)現(xiàn)AI識(shí)別圖片中的人物
最近碰到了照片識(shí)別的場(chǎng)景,正好使用了face_recognition項(xiàng)目,給大家分享分享。face_recognition項(xiàng)目能做的很多,人臉檢測(cè)功能也是有的,是一個(gè)比較成熟的項(xiàng)目。感興趣的可以了解一下2022-01-01
用python實(shí)現(xiàn)前向分詞最大匹配算法的示例代碼
這篇文章主要介紹了用python實(shí)現(xiàn)前向分詞最大匹配算法的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python 經(jīng)典貪心算法之Prim算法案例詳解
這篇文章主要介紹了Python 經(jīng)典貪心算法之Prim算法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
python Selenium爬取內(nèi)容并存儲(chǔ)至MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了python Selenium爬取內(nèi)容并存儲(chǔ)至MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-03-03

