Python爬取豆瓣視頻信息代碼實例
這篇文章主要介紹了Python爬取豆瓣視頻信息代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
這里是爬取豆瓣視頻信息,用pyquery庫(jquery的python庫)。
一:代碼
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)頁地址 參數(shù):搜索結(jié)果源代碼 返回值:最終的網(wǎng)頁地址 '
''
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)頁的源代碼 參數(shù):最終的地址 返回值:最終電影網(wǎng)頁的源代碼 '
''
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ù)功能:獲得每一項的數(shù)據(jù) 參數(shù):爬取頁面的源代碼 返回值:返回空 '
''
doc = pq(this_text)# 獲取標(biāo)題 title = doc(
'#content h1').text()# 獲取海報 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()# 豆瓣評分 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ù):爬取頁面的源代碼 返回值:返回空 '
''
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({
'觀看平臺': 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)頁地址 page_text2 =
the_last_page(last_url)# 獲得最終電影網(wǎng)頁的源代碼 the_last_text(
page_text2, my_str)# 獲得每一項的數(shù)據(jù) lookUrl(
page_text2, my_str)# 得到并處理觀看鏈接main()
二:結(jié)果如下(部分例子)
1.輸入天氣之子



2.輸入百變小櫻魔法卡



必須是已經(jīng)上映的電影才有觀看地址
3.獨立日





以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
tensorflow 1.X遷移至tensorflow2 的代碼寫法
本文主要介紹了tensorflow 1.X遷移至tensorflow2 的代碼寫法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
python 使用while循環(huán)輸出*組成的菱形實例
這篇文章主要介紹了python 使用while循環(huán)輸出*組成的菱形實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
淺談PyTorch中in-place operation的含義
這篇文章主要介紹了淺談PyTorch中in-place operation的含義,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python常用內(nèi)置函數(shù)和關(guān)鍵字使用詳解
在Python中有許許多多的內(nèi)置函數(shù)和關(guān)鍵字,它們是我們?nèi)粘V薪?jīng)??梢允褂玫牡降囊恍┗A(chǔ)的工具,可以方便我們的工作。本文將詳細講解他們的使用方法,需要的可以參考一下2022-05-05
Python本地搭建靜態(tài)Web服務(wù)器的實現(xiàn)
本文主要介紹了Python本地搭建靜態(tài)Web服務(wù)器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

