代码:
代码语言:javascript复制import requests
from bs4 import BeautifulSoup
import pymongo
import re
client = pymongo.MongoClient('localhost', 27017)
douban = client['douban']
top250 = douban['top250']
urls = ['https://movie.douban.com/top250?start={}'.format(str(i)) for i in range(0,250,25)]
def get_info(url):
wb_data = requests.get(url)
soup = BeautifulSoup(wb_data.text,'lxml')
names = soup.select('div.hd > a')
times = re.findall('<br>(.*?) ',wb_data.text,re.S)
places = re.findall(' / (.*?) / ',wb_data.text)
levels = soup.select('span.rating_num')
quotes = soup.select('span.inq')
for name,time,place,level,quote in zip(names,times,places,levels,quotes):
info = {
'name': name.get_text().split('/')[0].split('n')[1],
'time': time.split('n')[1].replace(' ',''),
'place': place,
'level': level.get_text(),
'quote': quote.get_text()
}
top250.insert_one(info)
for url in urls:
get_info(url)
实际爬取243条电影,出了一点小问题,建议大家爬取信息进入网站里面去爬会保险点,我这里懒得再重写了。然后导出excel表格,进行分析
简单分析
1.电影拿走不谢,请叫我雷锋 2.美国,日本,中国上榜电影拍前三 3.主要的电影内容:信仰,青春,科幻,情怀等 4.电影数最多的几年为1995~2013,近几年电影较少,原因大概为:虽然制片投入和电影效果越来越好,但内容却没以前那么好了。