{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "下载完成!\n" ] } ], "source": [ "import requests\n", "import re\n", "import csv, time\n", "\n", "# 创建一个保存的csv文件并设置好表头\n", "timenow = time.strftime(\"%Y-%m-%d-%H%M%S\", time.localtime())\n", "file = '猫眼电影top100榜-%s.csv' % (timenow)\n", "# 写入表头\n", "with open(file, 'a+', encoding='gb18030', newline='') as f:\n", " writer_f = csv.writer(f)\n", " writer_f.writerow(['排名', '电影名称', '主演', '上映时间', '评分'])\n", "\n", "\n", "# 定义一个爬取其中一页的电影信息\n", "def get_one_page(url):\n", " headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0'}\n", " response = requests.get(url, headers=headers).text\n", "\n", " pattern = re.compile(\n", " '