#!/usr/bin/env python # -*- coding:utf-8 -*- import requests import os # 若是url中的参数包含中文,那么须要先编码,不然对方服务器不识别 # 参数是中文的必须编码,requests包会自动编码 city = input("请输入城市名称:") url = "http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location=%E9%83%91%E5%B7%9E%E5%B8%82&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json" response = requests.get( url, params={"location":city} ) # response.json() 自动将响应数据解析为json对象 # 注意:数据格式必须知足json print(type(response.json())) # 也能够经过导入json包手动转换 # 导入文件通常写在文件的最上面 import json json_obj = json.loads(response.text) print(type(response.text)) json_obj = response.json() movie_list = json_obj.get("result").get("movie") with open("movie_info.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 idx, movie in enumerate(movie_list): # 电影图片,电影名称,评分,上映时间,分类,演员 ''' 电影图片,电影名称,评分,上映时间,分类,演员 ''' # python中注释只有#一种形式,三个单引号和三个双引号表示字符串,不叫注释 movie_picture = movie.get("movie_picture") movie_picture_response = requests.get(movie_picture) img_response = requests.get(movie_picture) movie_name = movie.get("movie_name") if not os.path.exists("imgs"): os.makedirs("imgs") with open("imgs/"+movie_name+".jpg","wb") as f1: f1.write(movie_picture_response.content) movie_score = movie.get("movie_score") movie_release_date = movie.get("movie_release_date") movie_tags = movie.get("movie_tags") movie_starring = movie.get("movie_starring") f.write("""<tr> <td>%s</td> <td><img src="%s" alt="蜘蛛侠" width="50px"></td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> </tr>""" % (idx+1, "imgs/"+movie_name+".jpg", movie_name, movie_score, movie_release_date, movie_tags, movie_starring[:10]+"...")) print(movie_picture, movie_name, movie_score, movie_release_date, movie_tags, movie_starring) f.write("""</table></div></body></html>""")