Python爬取豆瓣視頻信息代碼實(shí)例
這篇文章主要介紹了Python爬取豆瓣視頻信息代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
這里是爬取豆瓣視頻信息,用pyquery庫(kù)(jquery的python庫(kù))。
一:代碼
from urllib.request import quotefrom pyquery import PyQuery as pqimport requestsimport pandas as pddef get_text_page (movie_name): '' ' 函數(shù)功能:獲得指定電影名的源代碼 參數(shù):電影名 返回值:電影名結(jié)果的源代碼 ' '' url = 'https://www.douban.com/search?q=' + movie_name headers = { 'Host': 'www.douban.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36', } r = requests.get(url, headers = headers, timeout = 5) return r.textdef get_last_url( this_text): '' ' 函數(shù)功能:根據(jù)指定的源代碼得到最終的網(wǎng)頁(yè)地址 參數(shù):搜索結(jié)果源代碼 返回值:最終的網(wǎng)頁(yè)地址 ' '' doc = pq(this_text) lis = doc( '.title a').items() k = 0 this_str = '' for i in lis: #print('豆瓣搜索結(jié)果為:{0}'.format( i.text()))# print('地址為:{0}'.format(i.attr .href))# print('\n') if k == 0: this_str = i.attr.href k += 1 return this_strdef the_last_page( this_url): '' ' 函數(shù)功能:獲得最終電影網(wǎng)頁(yè)的源代碼 參數(shù):最終的地址 返回值:最終電影網(wǎng)頁(yè)的源代碼 ' '' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36', } r = requests.get(this_url, headers = headers, timeout = 20) return r.textdef the_last_text( this_text, movie_name): '' ' 函數(shù)功能:獲得每一項(xiàng)的數(shù)據(jù) 參數(shù):爬取頁(yè)面的源代碼 返回值:返回空 ' '' doc = pq(this_text)# 獲取標(biāo)題 title = doc( '#content h1').text()# 獲取海報(bào) photo = doc('.nbgnbg img') photo_url = photo.attr .src r = requests.get(photo_url) with open( '{m}.jpg'.format(m = movie_name), 'wb') as f: f.write(r.content)# 電影信息 message = doc('#info').text()# 豆瓣評(píng)分 grade = doc( '#interest_sectl').text()# 劇情 things = doc('.related-info').text() with open( '{0}.txt'.format(movie_name), 'w+') as f: try: f.writelines([title, '\n', '\n\n', message, '\n\n', grade, '\n\n', things ]) except: f.writelines([title, '\n', '\n\n', message, '\n\n', grade ])# 演員# 演員名 name = [] person_name = doc('.info').items() for i in person_name: name.append(i.text())# 演員圖片地址 person_photo = doc('#celebrities') j = 0 for i in person_photo.find('.avatar').items(): m = i.attr('style') person_download_url = m[m.find('(') + 1: m.find(')')]# 下載演員地址 r = requests.get(person_download_url) try: with open('{name}.jpg'.format(name = name[j]), 'wb') as f: f.write(r.content) except: continue j += 1 def lookUrl(this_text, my_str): '' ' 函數(shù)功能:獲得觀看鏈接 參數(shù):爬取頁(yè)面的源代碼 返回值:返回空 ' '' doc = pq(this_text) all_url = doc( '.bs li a').items() movie_f = [] movie_url = [] for i in all_url: movie_f.append(i.text()) movie_url .append(i.attr.href) dataframe = pd.DataFrame({ '觀看平臺(tái)': movie_f, '觀看地址': movie_url }) dataframe.to_csv( "{movie_name}的觀看地址.csv".format( movie_name = my_str), index = False, encoding = 'utf_8_sig', sep = ',') def main(): name = input('') my_str = name movie_name = quote(my_str) page_text = get_text_page(movie_name)# 得指定電影名的源代碼 last_url = get_last_url(page_text)# 根據(jù)指定的源代碼得到最終的網(wǎng)頁(yè)地址 page_text2 = the_last_page(last_url)# 獲得最終電影網(wǎng)頁(yè)的源代碼 the_last_text( page_text2, my_str)# 獲得每一項(xiàng)的數(shù)據(jù) lookUrl( page_text2, my_str)# 得到并處理觀看鏈接main()
二:結(jié)果如下(部分例子)
1.輸入天氣之子
2.輸入百變小櫻魔法卡
必須是已經(jīng)上映的電影才有觀看地址
3.獨(dú)立日
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python爬取你好李煥英豆瓣短評(píng)生成詞云的示例代碼
- python爬取豆瓣電影排行榜(requests)的示例代碼
- pycharm配置python 設(shè)置pip安裝源為豆瓣源
- 使用豆瓣源來(lái)安裝python中的第三方庫(kù)方法
- Python爬蟲(chóng)入門(mén)教程01之爬取豆瓣Top電影
- Python爬取豆瓣數(shù)據(jù)實(shí)現(xiàn)過(guò)程解析
- python使用re模塊爬取豆瓣Top250電影
- Python爬蟲(chóng)獲取豆瓣電影并寫(xiě)入excel
- Python利用Scrapy框架爬取豆瓣電影示例
- Python多線程爬取豆瓣影評(píng)API接口
- python 爬取豆瓣網(wǎng)頁(yè)的示例
相關(guān)文章
解決Python3中二叉樹(shù)前序遍歷的迭代問(wèn)題
二叉樹(shù)是分層數(shù)據(jù)結(jié)構(gòu),其中每個(gè)父節(jié)點(diǎn)最多有 2 個(gè)子節(jié)點(diǎn),在今天的文章中,我們將討論一個(gè)在大量技術(shù)編碼面試中出現(xiàn)的重要主題,對(duì)Python二叉樹(shù)遍歷相關(guān)知識(shí)感興趣的朋友一起看看吧2022-09-09tensorflow 1.X遷移至tensorflow2 的代碼寫(xiě)法
本文主要介紹了tensorflow 1.X遷移至tensorflow2 的代碼寫(xiě)法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12python 使用while循環(huán)輸出*組成的菱形實(shí)例
這篇文章主要介紹了python 使用while循環(huán)輸出*組成的菱形實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊
這篇文章主要給大家介紹了關(guān)于Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-01-01淺談PyTorch中in-place operation的含義
這篇文章主要介紹了淺談PyTorch中in-place operation的含義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python常用內(nèi)置函數(shù)和關(guān)鍵字使用詳解
在Python中有許許多多的內(nèi)置函數(shù)和關(guān)鍵字,它們是我們?nèi)粘V薪?jīng)??梢允褂玫牡降囊恍┗A(chǔ)的工具,可以方便我們的工作。本文將詳細(xì)講解他們的使用方法,需要的可以參考一下2022-05-05Python本地搭建靜態(tài)Web服務(wù)器的實(shí)現(xiàn)
本文主要介紹了Python本地搭建靜態(tài)Web服務(wù)器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02簡(jiǎn)單了解python中的f.b.u.r函數(shù)
這篇文章主要介紹了簡(jiǎn)單了解python中的f.b.u.r函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11