豆瓣电影简介查询器

利用爬虫获取豆瓣电影简介信息,其中增加了可视化功能!代码如下:

import tkinter as tk
import requests
from bs4 import BeautifulSoup
import json

header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'}
def search_movie():
    try:
        keyword = entry.get()
        url = f"https://movie.douban.com/j/subject_suggest?q={keyword}"
        response = requests.get(url=url, headers=header)
        output = response.text
        data = json.loads(output)
        id = data[0]['id']
        title = data[0]['title']
        new_url = f"https://movie.douban.com/subject/{id}/"
        response = requests.post(new_url, headers=header)
        content = response.text
        soup = BeautifulSoup(content, 'html.parser')
        head_contents = soup.find('head')
        description = get_info(head_contents)
        show_info(title, description)
    except Exception as e:
        show_info("错误", "查询出错了,换一个试试吧!!!!")


def get_info(head_contents):
    content = head_contents.find('script', {'type': 'application/ld+json'})
    if content:
        data = json.loads(str(content.string))
        description = data['description']
        return description


def show_info(movie, description):
    result_text.delete('1.0', tk.END)
    result_text.insert(tk.END, f"电影: {movie}\n电影简介: {description}")
    result_text.configure(font=("Arial", 12, "bold"))


if __name__ == '__main__':
    # 创建主窗口
    window = tk.Tk()
    window.title("电影查询")
    window.geometry("400x300")

    # 创建输入框和提示信息
    entry = tk.Entry(window, width=30)
    entry.insert(tk.END, "请输入电影名")
    entry.pack(pady=10)
    entry.configure(font=("Arial", 12, "bold"))

    # 绑定鼠标点击事件
    def clear_content(event):
        entry.delete(0, tk.END)

    entry.bind("<Button-1>", clear_content)

    def on_enter(event):
        search_movie()

    entry.bind("<Return>", on_enter)

    # 创建查询按钮
    button = tk.Button(window, text="查询", command=search_movie)
    button.configure(font=("Arial", 12, "bold"))
    button.pack(pady=5)

    # 创建简介描述框
    result_text = tk.Text(window, width=40, height=10)
    result_text.pack(pady=10)
    result_text.size = 20

    window.mainloop()

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
通过Python爬虫自建豆瓣电影API.zip 项目简介 在开发微信小程序「影库MDb」时,使用了豆瓣api来获取电影信息,后来由于豆瓣apikey失效了,小程序就无法正常访问,而个人主体貌似无法申请官方apikey,所以自己抽时间通过爬虫的方式写了个电影接口。这里把项目中的电影接口部分单独提取出来,分享给有需要的朋友。 本项目使用Python Flask框架搭建,采用爬取豆瓣官网的方式,获取并处理电影信息,返回的Json数据,便于小程序使用。 主要实现的功能有获取电影列表,获取电影信息、图片、演员等,以及获取影人信息、图片、作品,具体可参考api文档。 开发技术 编程语言:Python 3.7 WEB框架:Flask WSGI服务器:Waitress 使用方法 先部署环境,安装依赖。 pip install -r requirements.txt 直接运行run.py即可,我设置的5000端口,可改为其它,部署后可通过nginx来代理。 python run.py 成功运行之后可以通过 http://localhost:5000+对应URL和参数 来获取数据,URL和参数详见API文档。 建议直接部署使用,因为每次请求都要现爬取并处理数据,占用资源,响应比较慢,并且爬虫受限。可以在此基础上进行开发,将数据保存到本地数据库,然后定期更新数据。如果要部署使用,建议在config目录的配置文件里的HEADERS和PROXIES加上对应配置,来解决豆瓣爬虫限制。 使用建议 豆瓣官网有反爬机制,单ip有访问限制,所以建议增加ip代理池。 部分影视不登录豆瓣账号无法查看信息,所以建议增加cookie。 建议将爬取处理好的数据保存到本地数据库(推荐mongodb),处理逻辑是:访问接口->查询本地数据库->有返回数据,没有现爬数据并存储。 如果将数据存到本地,电影的一些数据会变化,建议添加个时间字段,用于定期更新数据。 由于我是在Windows服务器运行,使用的是Waitress,Linux建议更换其它WSGI服务器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛马程序员24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值