任务要求:
爬取豆瓣电影Top250的电影名、评分、短评、评分人数等信息
通过博客对beautifulSoup4的简单介绍,现在开始实战啦,没有看过的,可以先看看
推荐阅读:
直接上代码
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 22 12:03:06 2020
@author: kun
"""
import requests
from bs4 import BeautifulSoup
import pandas as pd
from fake_useragent import UserAgent
ua = UserAgent()
headers = {
'user-agent': ua.random,
'Host': 'movie.douban.com'
}
def get_movies():
movie_list = []
for i in range(0,10):
link = 'https://movie.douban.com/top250?start=' + str(i * 25)
r = requests.get(link, headers=headers, timeout= 10)
soup = BeautifulSoup(r.text, "lxml")
div_list = soup.find_all('div', class_='info')
for each in div_list:
title = each.find('div', class_='hd').a.span.text.strip()
info = each.find('div', class_='bd').p.text.strip()
info = info.replace("\n", " ").replace("\xa0", " ")
info = ' '.join(info.split())
rating = each.find('span', class_='rating_num').text.strip()
num_rating = each.find('div', class_='star').contents[7].text.strip()
try:
quote = each.find('span', class_='inq').text.strip()
except:
quote = ""
movie_list.append([title, info, rating, num_rating, quote])
df = pd.DataFrame(movie_list,columns=['电影名称', '信息', '评分', '评价人数', '短评'],index=None)
df.to_csv("douban.csv")
return movie_list
movies = get_movies()
print (movies)
到这里就结束了,如果对你有帮助你,欢迎点赞关注,你的点赞对我很重要