基于python的-json解析爬取电影接口(存入html中)

# -*- coding:utf-8 -*-

import requests
import os

city = input("请输入城市:")
url = "http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location=郑州市&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json"

response = requests.get(url, params={'location': city})
json_obj = response.json()

# error = json_obj['error']
# result = json_obj['result']
# movie_list = result['movie']
error = json_obj.get('error')

if error == 0:
    movie_list = json_obj.get('result').get('movie')
    with open('movie.html', 'w', encoding='utf-8') as f:
        f.write("""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>电影信息网</title>
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
</head>
<body>
    <div class="container">
        <table class="table table-bordered table-hover table-responsive">
            <tr>
                <td>ID</td>
                <td>电影图片</td>
                <td>电影名称</td>
                <td>上映时间</td>
                <td>国家</td>              
                <td>电影类型</td>
                <td>演员表</td>
            </tr>""")
        for id, movie in enumerate(movie_list):
            movie_picture = movie['movie_picture']
            picture_response = requests.get(movie_picture)
            movie_name = movie['movie_name']
            movie_release_date = movie['movie_release_date']
            movie_nation = movie['movie_nation']
            movie_tags = movie['movie_tags']
            movie_starring = movie['movie_starring']
            if not os.path.exists("images"):
                os.makedirs("images")
            with open("images/" + movie_name + ".jpg", "wb") as f1:
                f1.write(picture_response.content)
            f.write("""<tr>
                    <td>%s</td>
                    <td><img src="%s" alt="电影图片" width="80px"></td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                </tr>""" % (id+1, "images/"+movie_name+".jpg", movie_name, movie_release_date, movie_nation, movie_tags, movie_starring[:15] + '...'))
            print(movie_picture, movie_name, movie_release_date, movie_nation, movie_tags, movie_starring )
        f.write("""</table></div></body></html>""")
else:
    print('信息不存在!')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值