Python爬蟲日記一:爬取豆瓣電影中《戰(zhàn)狼2》演員圖片

一、前言

?? ? 參考了作者布咯咯_rieuse的《爬取豆瓣電影中速度與激情8演員圖片》一文,原文地址:爬取豆瓣電影中速度與激情8演員圖片 。于是開始學(xué)習(xí)、模仿、改進(jìn)。把自己學(xué)習(xí)的過程在此整理,與大家一起分享。
???? 目標(biāo):爬取圖片,并用對應(yīng)圖片的明星中文名字為文件名保存于電腦中。
???? 地址:戰(zhàn)狼2?


二、分析

本次爬取使用第三方的requests庫,同時也是使用了urllib.urlretrieve函數(shù)下載文件。
解析主要使用了BeautifulSoup,部分也使用了re正則表達(dá)式

1、導(dǎo)入需要的庫

import requests
from bs4 import BeautifulSoup

2、獲取地址,使用BS庫并使用lxml解析器

html=requests.get(url).content
soup=BeautifulSoup(html,'lxml')

3、抓取<title>標(biāo)簽中的片名作為文件的保存目錄

movie=soup.title.string.split(' ')[0]

4、抓取演員和對應(yīng)的圖片url

從下圖中分析可知中間的<div class="list-wrapper">對應(yīng)的才是所有演員的信息,所以我們用BS抓取中間的部分,代碼如下:

tags=soup.find_all(class_='list-wrapper')? #BS遍歷所有的list-wrapper類
starts=[]
for tag in tags[1].find_all('li')? ? ? ? ? ? ? ? ? ? ? #使用list-wrapper[1] 獲取對應(yīng)的演員的類,再次遍歷其下的li 標(biāo)簽
? ? title=tag.a['title'].split(' ')[0]? ? ? ? ? ? ? ? ? ? #獲取演員名字,去除后面英文名字
? ? img_url=re.findall(r'https://img\d.doubanio.com/img/celebrity/medium/.*.jpg',str(tag))[0]? #正則表達(dá)式獲取圖片信息,正則表達(dá)式返回列表,使用[0]獲取數(shù)據(jù)
???? stars.append([title,img_url])???????????????? #追加拼裝數(shù)據(jù)

三、完整代碼:

######1、geturl.py?

#coding=utf-8
import requests
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def geturl(url):
??? html=requests.get(url).content
??? soup=BeautifulSoup(html,'lxml')
??? return soup

######2、download_doupan.py

import geturl,re,urllib,os
soup=geturl.geturl('https://movie.douban.com/subject/20451290/celebrities')
movie=soup.title.string.split(' ')[0]
tags=soup.find_all(class_='list-wrapper')
stars=[]
for tag in tags[1].find_all('li'):
??? title=tag.a['title'].split(' ')[0]
??? img_url=re.findall(r'https://img\d.doubanio.com/img/celebrity/medium/.*.jpg',str(tag))[0]
??? stars.append([title,img_url])

if not os.path.exists(movie):
??? os.makedirs(movie)
??? for star in stars:
??????? filename=os.path.join(movie,star[0]+'.jpg')
??????? with open(filename,'w') as f:
??????????? urllib.urlretrieve(star[1],filename)



源碼鏈接Click Here



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容