首页 > 编程语言 >Python使用BeautifulSoup爬取人人影视的详情页面

Python使用BeautifulSoup爬取人人影视的详情页面

时间:2024-06-02 22:13:28浏览次数:10  
标签:Python BeautifulSoup getText 爬取 result div headItem select desc

import requests,json
from bs4 import BeautifulSoup

if __name__ == '__main__':
    url = "https://yyets.com/movies/201565/"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
    }
    result = {}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        head = soup.select("div#single div.sheader")
        for headItem in head:
            result['title'] = headItem.select("div.data h1")[0].getText()
            result['cover_url'] = headItem.select("div.poster img")[0]['src']
            result['date'] = headItem.select("div.extra span.date")[0].getText()
            result['classify'] = []
            for item in headItem.select("div.sgeneros a"):
                result['classify'].append(item.getText())

        info = soup.select("div#single div#info")
        for infoItem in info:
            desc = (infoItem.select("div.wp-content")[0]
                    .getText().strip())
            desc = desc.replace(desc[desc.rfind('\n'):], '')
            result['desc'] = desc
            result['pic_list'] = []
            g_imgs = infoItem.select("div#dt_galery img")
            for imgItem in g_imgs:
                img_src = imgItem['src'].strip()
                result['pic_list'].append(img_src)

        cast = soup.select("div#single div#cast")
        for castItem in cast:
            result['actor'] = []
            for actor in castItem.select("div.persons div.person div.data div.name a")[1:]:
                result['actor'].append(actor.getText())

        try:
            box_links = soup.select("div#single div.box_links")
            for linkItem in box_links:
                result['link'] = linkItem.select("div#videos table tr td a")[0]['href']
        except:
            result['link'] = ''

    with open('result.json', 'w', encoding='utf-8') as f:
        json.dump(result,f, ensure_ascii=False, indent=4)
        print("爬取成功!")

标签:Python,BeautifulSoup,getText,爬取,result,div,headItem,select,desc
From: https://www.cnblogs.com/skyvip/p/18227694

相关文章

  • 【Python基础】循环语句(5073字)
    文章目录@[toc]什么是循环Python中的循环方式while循环格式示例运行过程应用while循环嵌套示例1示例2for循环格式示例内置函数range()的用法range(x)range(x,y)range(x,y,z)应用break与continuebreakwhile循环中的break未使用break使用breakfor循环中的breakc......
  • python基础(习题、资料)
    免费提取资料: 练习、资料免费提取。持续更新迅雷云盘https://pan.xunlei.com/s/VNz6kH1EXQtK8j-wwwz_c0k8A1?pwd=rj2x#本文为Python的进阶知识合辑,包括列表(List)、元组(Tuple)、字典(Dictionary)和集合(Set)四种基本数据结构的介绍和实战案例分析。1、列表的简介列表(List)是一种用......
  • 天津科技-Python程序设计基础-数据结构-5
    编写函数avg(a),统计并返回元组a中所有奇数元素的平均值。在主程序中,从键盘输入(以空格分隔)包含若干个元素(数量不固定)的数值元组,调用avg()函数,计算并输出元组中所有奇数元素的平均值(保留2位小数)。输入格式:tuple(map(int,input().split()))输出格式"平均值为{:.2f}".format()......
  • 天津科技-Python程序设计基础-循环结构-5
    编写函数f(m,n),计算并返回。在主程序中输入m和n,调用函数f(),计算并输出函数值。输入格式:=map(int,input().split())输出格式:"{}".format()例如:输入1020输出8426deff(m,n):total=0forxinrange(m,n+1):total+=3*x**2+4*x+1r......
  • itertools: 一个实用的Python库
    很多人都致力于把Python代码写得更Pythonic,一来更符合规范且容易阅读,二来一般Pythonic的代码在执行上也更有效率。今天就先给大家介绍一个很Pythonic的Python系统库:itertools。itertools库迭代器(生成器)在Python中是一种很常用也很好用的数据结构,比起列表(list)来说,迭代器最大......
  • python自然语言处理实战:核心技术与算法 (涂铭,刘祥,刘树春)高清电子版pdf下载百度云
    书:pan.baidu.com/s/1rOoEvizAhkQyF8xScVh51w?pwd=8onw提取码:8onw我的阅读笔记:Python基础:为了进行NLP任务,首先需要掌握Python编程语言的基础知识。文本预处理:这包括文本清洗(如去除标点、停用词、特殊字符等)、文本分词(如中文分词)、文本向量化(如词袋模型、TF-IDF、Word2Vec等)。......
  • python系列&AI系列:Gradio库的安装和使用教程
    Gradio库的安装和使用教程Gradio库的安装和使用教程一、Gradio库的安装二、Gradio的使用1、导入Gradio库2、创建Gradio接口3、添加接口到Gradio应用4、处理用户输入和模型输出5、关闭Gradio应用界面三、Gradio的高级用法1、多语言支持2、自定义输入和输出格式3、模型版......
  • python系列&AIi系列(参考性极强):【完全攻略】Gradio:建立机器学习网页APP
    【完全攻略】Gradio:建立机器学习网页APP【完全攻略】Gradio:建立机器学习网页APP前言一、Gradio介绍以及安装1-1、Gradio介绍Gradio:1-2、安装二、快速开始(初步了解)2-1、简单小栗子2-2、多输入多输出2-3、简易聊天机器人三、关键技术3-1、带有样例的输入3-2、提示弹窗3-......
  • Docker 部署 Python 服务
    app.pyfromflaskimportFlaskfromredisimportRedis,RedisErrorimportosimportsocket#ConnecttoRedisredis=Redis(host="redis",db=0,socket_connect_timeout=2,socket_timeout=2)app=Flask(__name__)@app.route("/")defhe......
  • Python—面向对象小解(5)
    一、多任务介绍1.1进程与线程进程是操作系统分配资源的最小单元线程执行程序的的最小单元线程依赖进程,可以获取进程的资源一个程序执行先要创建进程分配资源,然后使用线程执行任务默认情况下一个进程中有一个线程1.2多任务介绍运行多个进程或线程执行代码逻辑......