豆瓣TOP250爬虫
import requests
from lxml import etree
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def spider_douban_top250():
movie_list_info = []
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}
for i in range(0, 250, 25):
url = 'https://movie.douban.com/top250?start={}&filter='.format(i)
# 获取bytes类型响应
data = requests.get(url, headers=header).content
html = etree.HTML(data)
ol_list = html.xpath('//div[@id="content"]//div[@class="article"]/ol/li')
for movie in ol_list:
# 影片序号
# serial_number
serial_number = movie.xpath('./div[@class="item"]/div[@class="pic"]/em/text()')[0]
# print('电影序号:', serial_number)
# 电影的名字
# movie_name
movie_name = movie.xpath('./div[@class="item"]/div[@class="info"]//a/span[1]/text()')[0]
# print('电影名字:', movie_name)
# 电影的介绍
# movie_introduce
movie_introduce = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[1]/text()')[0].strip()
movie_introduce = str(movie_introduce.replace('\xa0', ' '))
# print('电影介绍:', movie_introduce)
# 电影的星级
# movie_star
movie_star = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/div/span[2]/text()')[0]
# print('电影的星级:', movie_star)
# 电影的评价
# movie_evaluate
movie_evaluate = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/div/span[4]/text()')
movie_evaluate = movie_evaluate[0].replace('人评价', '')
# print('电影的评价:', movie_evaluate)
# 电影的描述
# movie_describe
movie_describe = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[2]/span[1]/text()')
# print('电影的描述:', movie_describe)
# 电影的图片
# movie_image
# 电影封面的地址
# movie_image_url
movie_image_url = movie.xpath('./div[@class="item"]/div[@class="pic"]/a/img/@src')[0]
# print('电影封面的地址:', movie_image_url)
movie_list_info.append({
'serial_number': serial_number,
'movie_name': movie_name,
'movie_introduce': movie_introduce,
'star': movie_star,
'evaluate': movie_evaluate,
'describe': movie_describe,
'movie_image_url': movie_image_url
})
for movie in movie_list_info:
print(movie)
# 下载图片
for movie in movie_list_info:
url = movie['movie_image_url']
resp = requests.get(url)
if resp.status_code == 200:
img_name = '0000000{}.jpg'.format(movie['serial_number'])
with open('./img/{}'.format(img_name), 'wb') as f:
f.write(resp.content)
spider_douban_top250()
绘制影片导演top10
intro_list = []
counts = {}
for ductor in movie_list_info:
ductor = ductor['movie_introduce'].split(' ')
counts[ductor[1]] = counts.get(ductor[1], 0) + 1
print(ductor)
print(counts)
# 排序 [(), ()]
ductor_items = list(counts.items())
ductor_items.sort(key=lambda x: x[1], reverse=True)
print(ductor_items)
nations = []
cishu = []
for i in range(10):
# 序列解包
ductors, count = ductor_items[i]
# print(role, count)
nations.append(ductors)
cishu.append(count)
explode = [0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
colors = ['red', 'purple', 'blue', 'yellow']
plt.pie(cishu, explode=explode, colors=colors, shadow=True, labels=nations, autopct='%1.1f%%')
plt.legend(loc=2) # 图例位置
plt.axis('equal')
plt.show()
图片的下载
# 图片的下载
import requests
url = 'http://g.hiphotos.baidu.com/image/pic/item/5366d0160924ab18014cefd83bfae6cd7a890b82.jpg'
response = requests.get(url)
# 获取bytes类型的相应
data = response.content
with open('风景.png', 'wb') as f:
f.write(data)