之前通過電影天堂+迅雷遠(yuǎn)程下載[xware]+阿里云服務(wù)器+百度云[bypy]共下載及上傳了近1000部電影爹脾,以供自己隨時可以可以通過百度云看以前的一些好看的電影~~
image.png
下載了這些電影后,想做個電影站玩玩,就想著資源該存放到哪里窖张,后來試用了下阿里云的oss,真心好用,不過價格也忒特么貴,雖然用cdn會劃算些富玷,但是~~還是貴,畢竟一部電影至少要1個G的流量既穆,著實耗不起~~
于是赎懦,使用了python的一個百度云插件bypy,將下載的電影全部上傳到百度云上了~~
后來又想著幻工,放在百度云上励两,但是不知道這部電影的評分怎么樣,于是乎就要去豆瓣上找囊颅,但是太麻煩了~~
后來当悔,就琢磨著做一個小程序,把百度云上的資源都放進去踢代,再寫個爬蟲根據(jù)電影名稱爬豆瓣的一些電影資料盲憎;
接下來,就有了這段代碼:
#-*- coding:utf-8 -*-
import sys
import requests
import urllib
import json
import re
"""
[{"episode":"","img":"https://img1.doubanio.com\/view\/photo\/s_ratio_poster\/public\/p2365823697.jpg","title":"冰川時代5:星際碰撞","url":"https:\/\/movie.douban.com\/subject\/25797778\/?suggest=%E5%86%B0%E5%B7%9D%E6%97%B6%E4%BB%A35","type":"movie","year":"2016","sub_title":"Ice Age: Collision Course","id":"25797778"}]
根據(jù)名稱獲取對應(yīng)的豆瓣ID->返回一個列表,默認(rèn)取第一個
"""
def get_id(keyword=None):
keyword = keyword.encode('utf-8') if isinstance(keyword,unicode) else keyword
q_word = urllib.quote(keyword)
url = 'https://movie.douban.com/j/subject_suggest?q={0}'.format(q_word)
#url = 'https://movie.douban.com/j/subject_suggest?q=%E5%86%B0%E5%B7%9D%E6%97%B6%E4%BB%A35'
resp = requests.get(url)
code = resp.status_code
if code != 200:
print 'get ID error'
return False
result = json.loads(resp.text)
#print json.dumps(result,ensure_ascii=False)
if result:
return result[0].get('id')
return None
"""
響應(yīng)體json序列化
"""
def get_detail_json(content=None):
key = '<script type="application/ld+json">'
cursor = content.find(key)
if not cursor:
return None
buf = content[cursor+len(key):]
key = '</script>'
cursor = buf.find(key)
buf = buf[:cursor].strip().replace('\n','')
try:
j = json.loads(buf)
except:
j = eval(buf)
return j
"""
獲取名稱
"""
def get_name(info={}):
return info.get('name')
"""
獲取豆瓣鏈接
"""
def get_douban_url(info={}):
url = 'https://movie.douban.com/{0}'.format(info.get("url"))
return url
"""
獲取圖片鏈接
"""
def get_image_url(info={}):
return info.get('image')
"""
獲取導(dǎo)演
"""
def get_directors(info={}):
L = info.get('directors') or []
names = [i.get('name') for i in L]
return names
"""
獲取編劇
"""
def get_authors(info={}):
L = info.get('author') or []
names = [i.get('name') for i in L]
return names
"""
獲取主演
"""
def get_actors(info={}):
L = info.get('actor') or []
names = [i.get('name') for i in L]
return names
"""
獲取上映日期
"""
def get_publish_date(info={}):
return info.get("datePublished")
"""
獲取影片類型
"genre": ["\u559c\u5267", "\u52a8\u753b", "\u5192\u9669"],
"""
def get_movie_type(info={}):
v = info.get("genre")
return v
"""
描述
"description": "影片講述松鼠奎特(克里斯?韋奇 Chris Wedge 配音)為了追松果胳挎,偶然引發(fā)了宇宙事件饼疙,改變并威脅著冰川時代的世界。為了拯救自己慕爬,話嘮樹懶希德(約翰?雷吉扎莫 John Leguizamo 配音...",
"""
def get_description(info={}):
return info.get("description")
"""
評分
"""
def get_score(info={}):
return info.get("aggregateRating",{}).get("ratingValue")
def get_info(info={}):
D = dict(
name = get_name(info),
image_url= get_image_url(info),
douban_url= get_douban_url(info),
directors= get_directors(info),
authors= get_authors(info),
actors= get_actors(info),
description= get_description(info),
publish_date= get_publish_date(info),
score= get_score(info),
types= get_movie_type(info),
)
return D
def get_detail(id=None):
url = 'https://movie.douban.com/subject/{0}/'.format(id)
resp = requests.get(url)
code = resp.status_code
if code != 200:
print 'get detail error'
return False
with open('./content.log', 'wb') as fp:
fp.write(resp.content)
info = get_detail_json(resp.text)
return get_info(info)
if __name__ == '__main__':
#1-根據(jù)名稱獲取 ID
id = get_id(sys.argv[1])
#2-根據(jù)ID獲取影片內(nèi)容
resp=get_detail(id)
for i in resp:
print i, json.dumps(resp[i],ensure_ascii=False)
小程序后來也做了窑眯,也加了一部分電影進去,效果如下:
【列表頁】
image.png
【影片介紹】
image.png
【百度云鏈接復(fù)制】
image.png
【然后直接打開百度云就能找到電影并觀看了】
image.png
用著確實很方便哈医窿,但是整理起來也挺麻煩磅甩,所以整理了一部分就放棄了~~~~
在此,僅分享下豆瓣電影信息的爬取哈姥卢;