爬取数据并写入Excel表格——猫眼电影的爬取

from bs4 import BeautifulSoup
import requests
import xlwt
import os

def get_page(pages):
    offset = pages*10
    url = "https://maoyan.com/board/4?offset=" + str(offset)
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"
    }
    try:
        res = requests.get(url, headers=headers)
        if res.status_code == 200:
            return res.text
    except requests.exceptions.RequestException as e:
        print("Error:",e.args)
        
def parse_page(html):
    soup = BeautifulSoup(html, "lxml")
    all_movies_details = soup.find_all(name="div", class_ = "board-item-content")
    for lins,each in enumerate(all_movies_details):
        name = each.find(class_="name").a.string
        actors = each.find(class_="star").string.strip(" \n")[3:]
        releasetime = each.find(class_="releasetime").string[5:]
        score = each.find(class_="score").get_text()
        details = [name, actors, releasetime, score]
        print(details)
        yield details
        
def write_to_excle():
    mysheet = xlwt.Workbook()
    sheet = mysheet.add_sheet("sheet1")
    title=["电影名", "主演", "上映时间", "评分"]
    for col,head in enumerate(title):
        sheet.write(0,col,head)
    for page in range(0,10):
        html = get_page(page)
        details =parse_page(html)
        start = page*10+1
        for row,con in enumerate(details,start):
            for col,art in enumerate(con):
                sheet.write(row,col,art) 
    mysheet.save("maoyan.xls")
def main():
    write_to_excle()
    
main()

战果

爬取数据并写入Excel表格——猫眼电影的爬取_第1张图片

你可能感兴趣的:(爬虫)