溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python爬取豆瓣視頻信息代碼實(shí)例

發(fā)布時間:2020-10-23 18:59:05 來源:腳本之家 閱讀:143 作者:一只帥氣的IT小昂 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Python爬取豆瓣視頻信息代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這里是爬取豆瓣視頻信息,用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ù)功能:獲得每一項(xiàng)的數(shù)據(jù)  參數(shù):爬取頁面的源代碼  返回值:返回空  '
''
doc = pq(this_text)# 獲取標(biāo)題 title = doc(
		'#content h2').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()# 豆瓣評分 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)# 獲得每一項(xiàng)的數(shù)據(jù) lookUrl(
		page_text2, my_str)# 得到并處理觀看鏈接main()

二:結(jié)果如下(部分例子)

1.輸入天氣之子

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

2.輸入百變小櫻魔法卡

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

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

3.獨(dú)立日

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

Python爬取豆瓣視頻信息代碼實(shí)例

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI