flask制作电影天堂的API接口

flask制作电影天堂的API接口

from flask import Flask, request
import json
from lxml import etree
import requests


#根据自己访问的页面进行url调节
url = "https://www.dytt8.net/html/gndy/dyzz/list_23_1.html"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
}


def get_first_url(url):
    resp = requests.get(url, headers=headers)
    html = etree.HTML(resp.text)
    movie_href = html.xpath('//table[@class="tbspan"]//a/@href')
    basic_domain = 'https://www.dytt8.net'
    first_url = []
    for href in movie_href:
        first_url.append(basic_domain + href)
        # print(basic_domain + href)
    return first_url


def get_detail_html(move_list):
    all_movie_list = []
    for movie in move_list:
        detail_html = requests.get(movie, headers=headers)
        html_content = detail_html.content.decode('gb2312', 'ignore')
        html_e = etree.HTML(html_content)
        movie_information = {}
        title = html_e.xpath('//title/text()')[0]
        movie_information['title'] = title
        img_srcs = html_e.xpath('//img/@src')
        movie_information['img'] = img_srcs[0]
        elements = html_e.xpath('//p[position()=1]')
        ele_info = elements[0].xpath('//br')
        index = 0
        for ele in ele_info:
            str_ele = etree.tostring(ele, encoding='utf-8').decode('utf-8')
            if str_ele.startswith('<br />◎译  名'):
                name = str_ele.replace("<br />◎译  名", "").strip()
                movie_information['name'] = name
            elif str_ele.startswith('<br />◎片  名'):
                english_name = str_ele.replace('<br />◎片  名', '').strip()
                movie_information['English_name'] = english_name
            elif str_ele.startswith('<br />◎产  地'):
                palace = str_ele.replace("<br />◎产  地", '').strip()
                movie_information['palace'] = palace
            elif str_ele.startswith('<br />◎类  别'):
                type_movie = str_ele.replace('<br />◎类  别', '').strip()
                movie_information['move_type'] = type_movie
            elif str_ele.startswith('<br />◎编  剧'):
                authors = []
                author = str_ele.replace('<br />◎编  剧', '').strip()
                # print(actor)
                authors.append(author)
                # print(ele_info.index(ele))
                for i in range(ele_info.index(ele), len(ele_info)):
                    str_ele2 = etree.tostring(ele_info[i + 1], encoding='utf-8').decode('utf-8')
                    if str_ele2.startswith('<br />◎主  演') == False:
                        clean_author = str_ele2.replace('<br />', '').strip()
                        authors.append(clean_author)
                    else:
                        movie_information['authors'] = authors
                        # print(movie_information)
                        break
            elif str_ele.startswith('<br />◎主  演'):
                actors = []
                actor = str_ele.replace('<br />◎主  演', '').strip()
                # print(actor)
                actors.append(actor)
                # print(ele_info.index(ele))
                for i in range(ele_info.index(ele), len(ele_info)-1):
                    str_ele2 = etree.tostring(ele_info[i + 1], encoding='utf-8').decode('utf-8')
                    if str_ele2.startswith('<br />◎标  签') == False:
                        clean_actor = str_ele2.replace('<br />', '').strip()
                        actors.append(clean_actor)
                    else:
                        movie_information['actors'] = actors
                        break
                        # print(movie_information)
            # print(english_name)
        # print(etree.tostring(ele, encoding='utf-8').decode('utf-8'))
        all_movie_list.append(movie_information)
        # break
    json_out = json.dumps(all_movie_list,ensure_ascii=False)
    return json_out

app = Flask(__name__)


# 只接受get方法访问
@app.route("/test_1.0", methods=["GET"])
def check():
    my_info = tt();
    return my_info


# 功能函数
def tt():
    move_list = get_first_url(url)
    info_value = get_detail_html(move_list)
    return info_value


if __name__ == "__main__":
    app.run(debug=True)

get_detail_html(move_list)
return info_value

if name == “main”:
app.run(debug=True)


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200417174715679.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvdmVTdGFyYnVja3M=,size_16,color_FFFFFF,t_70)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值