爬取中国票房网的年度票房的电影票房数据

import requests
import xlwt
import json
columns=['影片名称', '影片类型', '总票房(万)', '平均票价', '场均人次', '国家及地区','上映日期']
workbook = xlwt.Workbook(encoding="utf-8")
worksheet = workbook.add_sheet('My Worksheet')
for k in range(len(columns)):
   worksheet.write(0,k,columns[k])
   workbook.save('movie.xls')
def main(year_shu):
    url='https://www.endata.com.cn/API/GetData.ashx'
    i = 1
    for h in range(year_shu):
        j = 2020 - h
        k = {
     'year': j,
             'MethodName': 'BoxOffice_GetYearInfoData'}
        header = {
     'Host': 'www.endata.com.cn',
                  'Origin': 'https://www.endata.com.cn',
                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
                                'AppleWebKit/537.36 (KHTML, like Gecko)'
                                ' Chrome/85.0.4183.83' 'Safari/537.36'}
        response = requests.post(url=url, headers=header,  data=k)
        print(response.text)
        red = []
        c = json.loads(response.text)
        lst1 = c['Data']
        lst = lst1['Table']
        for item in lst:
            for j in range(7):
                if j == 0:
                    worksheet.write(i, j, item["MovieName"])
                    workbook.save('movie1.xls')
                if j == 1:
                    worksheet.write(i, j, item["Genre_Main"])
                    workbook.save('movie1.xls')
                if j == 2:
                    worksheet.write(i, j, item["BoxOffice"])
                    workbook.save('movie1.xls')
                if j == 3:
                    worksheet.write(i, j, item["AvgPrice"])
                    workbook.save('movie1.xls')
                if j == 4:
                    worksheet.write(i, j, item["AvgPeoPle"])
                    workbook.save('movie1.xls')
                if j == 5:
                    worksheet.write(i, j, item["Area"])
                    workbook.save('movie1.xls')
                if j == 6:
                    worksheet.write(i, j, item["ReleaseTime"])
                    workbook.save('movie1.xls')
            i = i + 1
if __name__ == '__main__':
    year_shu = int(input("请输入几年内:"))
    main(year_shu)

可直接运行,生出movie1.xls文件

你可能感兴趣的:(python)