爬虫练习:获取豆瓣top250的电影

该代码实现从豆瓣网站抓取Top250电影的名称、评分、链接和推荐语,并将这些信息存储到CSV文件中。通过requests库发送HTTP请求,使用BeautifulSoup解析HTML内容,最后将数据写入CSV文件。
摘要由CSDN通过智能技术生成
from bs4 import BeautifulSoup
import requests
import csv

'''获取页面内容'''


def get_url_content(page):
'''伪装报头'''
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/83.0.4103.61 Safari/537.36 '
    }
    response = requests.get("https://movie.douban.com/top250?start=" + str(page), headers=headers)
    return response


'''分析页面内容,找到电影的名称、评分、链接、推荐语'''


def get_movie_table(response):
    soup = BeautifulSoup(response.content, "html5lib")
    movies = soup.find_all('div', class_='item')
    movies_table = [['名称', '评分', '链接', '推荐语']]
    for item in movies:
        try:
            name = item.find('div', class_='info').find('span', class_='title').text
        except BaseException:
            name = ''
        try:
            star = item.find('div', class_='info').find('span', class_='rating_num').get_text()
        except BaseException:
            star = ''
        try:
            link = item.find('div', class_='info').find('a')['href']
        except BaseException:
            link = ''
        try:
            comment = item.find('div', class_='info').find('span', class_='inq').get_text()
        except BaseException:
            comment = ''
        movies_table.append([name, star, link, comment])
    return movies_table


if __name__ == '__main__':
    movie_table = []
    '''点击下一页,观察url的变化,发现page的间隔是25,开始于0,结束于255'''
    for page in range(0, 226, 25):
        temp_movies_table = get_movie_table(get_url_content(page))
        movie_table.extend(temp_movies_table)
    print('获取信息完成')
    '''将生成的列表存储在csv中,文件位于项目文件夹里'''
    try:
        with open('movies.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)
            for row in movie_table:
                writer.writerow(row)
        print("存储完成")
    except BaseException:
        print('存储失败')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值