python学习第四天

作业

  • 写爬虫爬取豆瓣重庆影讯https://movie.douban.com/cinema/later/chongqing

内容

  • 导入包
import requests #请求
from lxml import html#解析
from matplotlib import pyplot as plt#绘图
#绘图汉字编码
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

-导入域名

url ='http://movie.douban.com/cinema/later/chongqing/'#豆瓣重庆影讯

-将爬取到的电影信息按"想看人数的由多到少"排序输出

info_list=[]#存电影信息
headers = {
    "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"}
resp = requests.get(url, headers=headers)
html_data = resp.text

with open('douban.html','w',encoding='utf-8') as f:
    f.write(html_data)

# print(resp.text)


selector = html.fromstring(html_data)
movie_list = selector.xpath('//div[@id="showing-soon"]/div')
print(movie_list)

name_list = selector.xpath('//div[@id="showing-soon"]/div/div/h3/a/text()')
# print(name_list)
cishu=0
for i in movie_list:
    # 电影名称
    name=i.xpath('//div[@id="showing-soon"]/div/div/h3/a/text()')[cishu]
    # print(name)
    # 上映日期
    date=i.xpath('//div[@id="showing-soon"]/div/div/ul/li[1]/text()')[cishu]
    # 类型
    type=i.xpath('//div[@id="showing-soon"]/div/div/ul/li[2]/text()')[cishu]
    # 上映国家
    country=i.xpath('//div[@id="showing-soon"]/div/div/ul/li[3]/text()')[cishu]
    # 想看人数

    people=i.xpath('//div[@id="showing-soon"]/div/div/ul/li[4]/span/text()')[cishu]
    # cishu+=1
    people=int(people.replace('人想看','   '))
    info_list.append({
        'name':name,
        'date':date,
        'type':type,
        'country':country,
        'people':people
    })
    # print(info_list)
    cishu += 1
info_list.sort(key=lambda x:x['people'], reverse=True)
for m in info_list:
    print(m)

-绘制上映电影国家占比图

#上映国家
country_list=[]
for i in info_list:
    country_list.append(i['country'])
cou = {}#将国家与次数弄成一个键值对
for word in country_list:
    cou[word]=cou.get(word, 0)+1
print(len(cou))
times = list(cou.items())
coun=[]#国家放在一个列表
cons=[] #次数放在一个列表
for i in range(len(cou)):
    guojia,ci=times[i]
    guojia=str(guojia)
    ci=int(ci)
    coun.append(guojia)
    cons.append(ci)

print(coun)#输出国家
print(cons)#输出次数
#绘制占比图
# 距离圆心点距离
explode = [0.1, 0, 0, 0]
colors = ['red', 'purple', 'blue', 'yellow']
plt.pie(cons, explode=explode, shadow=True, labels=coun, autopct='%1.1f%%', colors=colors)
plt.legend(loc=2)
plt.axis('equal')
plt.show()

-占比图:


python学习第四天_第1张图片
上映电影国家占比图

-绘制top5最想看电影
上面已经给电影信息排过名了

top5_movie = [info_list[i] for i in range(5)]
print(top5_movie)
x=[x['name'] for x in top5_movie]

y=[x['people'] for x in top5_movie]

plt.barh(x,y)
plt.show()
  • Top5最想看电影图:


    python学习第四天_第2张图片
    Top5最想看电影图

你可能感兴趣的:(python学习第四天)