欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python爬取电影top存入HTML文件

程序员文章站 2022-07-14 10:18:59
...

   当我们在进行爬虫的时候!会选择多种存储显示方式!这次我们简单的实例一下(需要一点HTML基础)

关于HTML布局推荐网站https://v3.bootcss.com/     ,需要在同步文件夹里添加css文件,HTML引入

py文件代码

from lxml import etree
import requests
import json
from urllib.request import urlretrieve
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'
req = requests.get(url).text
# print(req)
# print(type(req))
data= json.loads(req)
# print(data['result']['movie'])
movie_name_list=[]   #电影名
movie_release_date=[]  #上映时间
movie_starring=[]    #主演
movie_picture=[]     #海报
movie_score=[]    #评分
for x in data['result']['movie']:
    # print(x)
    # print(x['movie_name'])#电影名
    movie_name_list.append(x['movie_name'])
    movie_release_date.append(x['movie_release_date'])
    movie_starring.append(x['movie_starring'])
    movie_picture.append(x['movie_picture'])
    movie_score.append(x['movie_score'])
# print(movie_release_date[20])
# print(type(movie_picture))
# for z in range(0,len(movie_picture)):
#     urlretrieve(movie_picture[z],'C:\\Users\\Administrator\\Desktop\\movie\\'+'{}.jpg'.format(z))
with open('movie.html','w',encoding="utf-8")as f:
    f.write('''
        <!doctype html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport"
                  content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
            <title>最新电影</title>
            <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
        </head>
        <body>
             <table class="table table-bordered">
                    <tr>
                        <td>id</td>
                        <td>电影图片</td>
                        <td>上映时间</td>
                        <td>电影名</td>
                        <td>主演</td>
                        <td>评分</td>
                    </tr>
    ''')
    f.close()
    for y in range(0,len(movie_name_list)):
        with open('movie.html','a',encoding='utf-8')as f:
            f.write(
                    '<tr>'+'\n'
                    '<td>'+str(y)+'</td>'+'\n'
                    '<td>'+'<img'+' src="'+'movie'+'\\{}.jpg"'.format(y)+' alt="" width="50%">'+'</td>'+'\n'
                    '<td>'+movie_release_date[y]+'</td>'+'\n'
                    '<td>'+movie_name_list[y]+'</td>'+'\n'
                    '<td>'+movie_starring[y]+'</td>'+'\n'
                    '<td>'+movie_score[y]+'</td>'+'\n'
                    '</tr>'+'\n'
            )
            f.close()
with open('movie.html','a',encoding="utf-8")as f:
    f.write('''
       </table></body></html>
    ''')
f.close()

html代码

主要添加表格代码

python爬取电影top存入HTML文件

 建立好表格之后才好向里面添加东西

结果

python爬取电影top存入HTML文件

 上图只是基础的HTML表格,对着方面有研究的大佬们,可以选择尽情美化!!!!!

相关标签: 小白

上一篇: hive

下一篇: Quartz学习总结