会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132578个问题
JAVA 全系列/第三阶段:数据库编程/JDBC技术 26026楼

# _*_coding=utf-8 _*_
from fake_useragent import UserAgent
import requests
from lxml import etree
from time import sleep

def get_html(url):
    """
    :param url: 要爬取的url
    :return返回html
    """
    headers = {
        "User-Agent": UserAgent().chrome
    }
    resp = requests.get(url, headers=headers)
    sleep(3)
    if resp.status_code == 200:
        resp.encoding = 'utf-8'
        return resp.text
    else:
        return None

def parse_list(html):
    """
    :param html: 传递进来一个有电影列表的html
    :return 返回一个电影列表的url
    """
    e = etree.HTML(html)
    list_url = ['https://maoyan.com'+ url for url in e.xpath('//div[@class="movie-item-hover"]/a/@href')]
    return list_url

def parse_index(html):
    """
    :param html: 传递进来一个有电影信息的url
    :return  已经提取好的电影信息
    """
    e = etree.HTML(html)
    names = e.xpath('//h1/text()')[0]
    type = e.xpath('//li[@class="ellipsis"]/a/text()')[0]
    actor = e.xpath('//ul[@class="celebrity-list clearfix"]/li[@class="celebrity actor"]/div/a/text()')
    actors = format_actor(actor)
    return {'name': names, 'type': type, 'actor': actors}

def format_actor(actors):
    actor_set = set()  # 去重
    for actor in actors:
        actor_set.add(actor.strip())
    return actor_set

def main():
    num = int(input('请输入要获取多少页数据'))
    for y in range(num):
        url = 'https://maoyan.com/films?showType=3&offset={}'.format(y*30)
        # print(url)
        list_html = get_html(url)
        list_url = parse_list(list_html)
        for url in list_url:
            # print(url)
            info_html = get_html(url)
            movie = parse_index(info_html)
            print(movie)


if __name__ == '__main__':
    main()

image.png

老师为啥没有数据啊!

Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 26028楼

import pymysql

class MyDb:
    config={
        "host":"localhost",
        "user":"root",
        "password":"123456",
        "db":"music_project",
        "charset":"utf8"
    }

    def __init__(self):
        self.connection = pymysql.connect(**DbUtils.config)
        self.cursor = self.connection.cursor()
    
    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.close()

    #插入,修改,删除
    def exeDML(self,sql,*args):
        try:
            #执行sql
            count = self.cursor.execute(sql,args)
            id = self.connection.insert_id()
            #提交事务
            self.connection.commit()
            return id
        except Exception as e:
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()

    #查询一条
    def queryone(self,sql,*args):
        try:
            #执行sql
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()

    #查询多条
    def queryall(self,sql,*args):
        try:
            #执行sql
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchall()    
        except Exception as e:
            print(e)
        finally:
            self.close()

if __name__ == "__main__":
    dbutils = MyDb()
    #测试exeDML
    # sql = "insert into emp(empno,ename,sal) values(%s,%s,%s)"
    # count = dbutils.exeDML(sql,6666,'阿巴阿巴',9999)
    # print(count)

    # sql = "select * from emp where empno=%s"
    # count = dbutils.queryone(sql,7788)
    # print(count)

    sql = "select * from t_music"
    emps = dbutils.queryall(sql)
    for i in emps:
        print(i)

图片.png

老师我也报了和评论一样的错误,可是我对照了一下资料的代码,发现好像一样啊,为什么会出现这种情况啊

Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 26029楼
JAVA 全系列/第四阶段:网页编程和设计/Javascript 语言(旧) 26030楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 26031楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 26032楼
WEB前端全系列/第二十阶段:Vue2企业级项目(旧)/易购商品后台管理系统 26033楼
JAVA 全系列/第四阶段:网页编程和设计/CSS3(旧) 26036楼
Python 全系列/第一阶段:Python入门/控制语句 26039楼
JAVA 全系列/第六阶段:项目管理与SSM框架/RBAC实战 26040楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637