python-爬蟲-貓眼電影TOP100

#!/usr/bin/env python
#-*- coding:utf8 -*-
#__author__ = "LiDaguo"


import requests
import re
import xlwt

url = 'https://maoyan.com/board/4?'


headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
}   # 請求頭



def get_page(url):
    '''輸入網址-返回網頁內容'''
    try:
        response = requests.get(url, headers=headers)
        if response.status_code == 200: # 看看是不是正常訪問
            return response.text
        else:
            print('獲取網頁失敗')
    except Exception as e:
        print(e)


def get_info(page):
    '''得到網頁-提取信息'''

    items = re.findall('board-index .*?>(\d+)</i>.*?class="name"><.*?>(.*?)</a></p>.*?<p class="star">.*?' +
                       '主演:(.*?) .*?</p>.*?<p class="releasetime">(.*?)</p>.*?<p class="score"><i class="integer">' +
                       '(.*?)</i><i class="fraction">(\d+)</i></p>', page, re.S)    # 構造正則表達式
    # items是個列表,列表裏的每個元素是個元組。每個元組裏都包含各個電影的名稱、演員、上映時間等信息
    for item in items:
        data = {}
        data['rank'] = item[0]
        data['title'] = item[1]
        actors = re.sub('\n', '', item[2])
        data['actors'] = actors
        data['date'] = item[3]
        data['score'] = str(item[4]) + str(item[5])
        yield data


urls = ['https://maoyan.com/board/4?offset={}'.format(i * 10) for i in range(1)]
DATA = []


for url in urls:
    page = get_page(url)
    datas = get_info(page)      # datas是一個生成器,不是具體數
    for item in datas:      # 把生成器放到for語句中:每次的item都是生成器返回的值,這兩個for循環遙相呼應
        DATA.append(item)  # 將所有的數據添加到DATA裏

f = xlwt.Workbook(encoding='utf-8')     # 創建表格
sheet01 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)    # 命名sheet
# 表頭數據寫入excle
sheet01.write(0, 0, 'rank')     # 第1行第1列
sheet01.write(0, 1, 'title')    # 第1行第2列
sheet01.write(0, 2, 'actors')
sheet01.write(0, 3, 'date')
sheet01.write(0, 4, 'score')
# 寫內容
for i in range(len(DATA)):
    sheet01.write(i + 1, 0, DATA[i]['rank'])
    sheet01.write(i + 1, 1, DATA[i]['title'])
    sheet01.write(i + 1, 2, DATA[i]['actors'])
    sheet01.write(i + 1, 3, DATA[i]['date'])
    sheet01.write(i + 1, 4, DATA[i]['score'])
    print('爬取完成', end='')

f.save('E:\\貓眼電影.xls')





效果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章