在python中使用网络抓取数据的条形图

时间:2019-03-20 23:59:10

标签: python pandas bar-chart

由于某种原因,我的代码没有输出。

import requests
import bs4
import pandas
import matplotlib.pyplot as plt

res = requests.get('https://www.imdb.com/search/title?title_type=feature,documentary,short&release_date=2018-01-01,2018-12-31')
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')

moviecontainers = soup.find_all('div', class_ = 'lister-item mode-advanced')

titles = []
ratings = []
metascores = []

for container in moviecontainers:
  if container.find('div', class_ = 'ratings-metascore'):
    name = container.h3.a.text
    titles.append(name)

    rating = float(container.strong.text)
    ratings.append(rating)

    metascore = container.find('span', class_ = 'metascore').text
    metascores.append(metascore)

movies = pandas.DataFrame({'movie title': titles,
          'imdb ratings': ratings,
          'metascore': metascores
})

ax = movies[['imdb ratings','metascore']].plot(kind='bar', title ="Rating comp", figsize=(15, 10), legend=True)
ax.set_xlabel("Movie Titiles")
ax.set_ylabel("Ratings")
plt.show()

谁能告诉我哪里出了问题?我认为它主要是图形部分,因为我尝试在没有条形图代码的情况下运行它。

0 个答案:

没有答案