# -*- 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('信息不存在!')
基于python的-json解析爬取电影接口(存入html中)
最新推荐文章于 2024-05-02 11:55:28 发布