一、绘图补充
1. 绘制水平柱状图
# 使条形图在y轴显示,水平条形图
plt.barh(x, y)
plt.ylabel('口红品牌')
plt.xlabel('口红价格(元)')
plt.show()
水平柱状图
- 绘制饼图
# 饼图的绘制
from random import randint
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# x是饼图中各个元素的值
x = [randint(3000, 15000) for _ in range(8)]
# 每个元素的标签
labels = ['员工{}'.format(i) for i in range(1, 9)]
colors = ['red', 'blue', 'green', 'yellow', 'pink', 'purple', 'grey', 'orange']
# 每一个元素距离中心点的距离 0~1
explode = [0, 0, 0, 0, 0, 0, 0, 0.3]
plt.pie(x=x,
labels=labels,
colors=colors,
shadow=True,
startangle=90, # 开始的角度 ,例如90度,从最上方开始
explode=explode,
autopct='%1.1f%%' # 显示百分比
)
plt.axis('equal') # 设置成标准圆形
plt.legend(loc=2) # (location)制定为2象限
plt.title('某大型公司员工工资占比')
plt.show()
饼状图
二、切片
格式
对象[起始位置:终止位置:步长]
左闭右开,起始和终止可以省略不写
示例:
# # 切片
# name = 'wozuishuai'
# # 切片语法
# # 对象[起始位置:终止位置:步长]
# # 左闭右开,起始和终止可以省略不写
# print(name[2::2]) # ziha
# print(name[:4]) # wozu
# stu_list = ['学生{}'.format(i) for i in range(20)]
# print(stu_list)
# # 筛选5~15位同学
# print(stu_list[4:15])
l1 = [
{'name': '张三', 'grade': 56},
{'name': '李四', 'grade': 90},
{'name': '王五', 'grade': 6},
{'name': '赵六', 'grade': 33},
{'name': '田七', 'grade': 67},
{'name': '老八', 'grade': 12},
]
# print(l1[:3])
# 分别取出 每个字典中的名字
# d = "{'name': '张三', 'grade': 56}"
# print(d['name'])
print([x['grade'] for x in l1[:3]]) # [56, 90, 6]
三、练习:豆瓣电影TOP250爬虫(自己代码)
url = 'https://movie.douban.com/top250'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}
import requests
from lxml import html
import pandas as pd
# 请求url
response = requests.get(url, headers=headers)
selector = html.fromstring(response.text)
# 用xpath语法抓取数据放入容器中
li_list = selector.xpath('//div[@id="content"]/div[@class="grid-16-8 clearfix"]/div[@class="article"]/ol[@class="grid_view"]/li')
print('共爬取了{}部电影.'.format(len(li_list)))
movie_list = []
for li in li_list:
movie_img = li.xpath('div[@class="item"]/div[@class="pic"]/a/img/@src')
movie_img = '找不到数据' if len(movie_img)==0 else movie_img[0]
# print(movie_img)
movie_id = li.xpath('div[@class="item"]/div[@class="pic"]/em/text()')
movie_id = '找不到数据' if len(movie_id) == 0 else int(movie_id[0])
# print(movie_id)
movie_name = li.xpath('div[@class="item"]/div[@class="info"]/div[@class="hd"]/a/span[1]/text()')
movie_name = '找不到数据' if len(movie_name) == 0 else movie_name[0]
# print(movie_name)
movie_link = li.xpath('div[@class="item"]/div[@class="info"]/div[@class="hd"]/a/@href')
movie_link = '找不到数据' if len(movie_link) == 0 else movie_link[0]
# print(movie_link)
movie_info = li.xpath('div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[1]/text()')
movie_info = '找不到数据' if len(movie_info) == 0 else movie_info[0].strip()
# print(movie_info)
movie_rating_num = li.xpath('div[@class="item"]/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[@class="rating_num"]/text()')
movie_rating_num = '找不到数据' if len(movie_rating_num) == 0 else float(movie_rating_num[0])
# print(movie_rating_num)
movie_rating_people = li.xpath('div[@class="item"]/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[4]/text()')
movie_rating_people = '找不到数据' if len(movie_rating_people) == 0 else int(movie_rating_people[0].replace('人评价', ''))
# print(movie_rating_people)
movie_quote = li.xpath('div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[@class="quote"]/span/text()')
movie_quote = '找不到数据' if len(movie_quote) == 0 else movie_quote[0]
# print(movie_quote)
movie_list.append({
'movie_img': movie_img,
'movie_id': movie_id,
'movie_name': movie_name,
'movie_link': movie_link,
'movie_info': movie_info,
'movie_rating_num': movie_rating_num,
'movie_rating_people': movie_rating_people,
'movie_quote': movie_quote
})
# print(movie_list)
# 存入本地
pd.DataFrame(movie_list).to_csv('豆瓣top250.csv')
for movie in movie_list:
print(movie)
with open('./douban_top250_img/{}.jpg'.format(movie['movie_name']), 'wb') as f:
f.write(requests.get(movie['movie_img'], headers=headers).content)
豆瓣电影TOP250.csv
TOP250电影封面









网友评论