会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132886个问题
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 30841楼

老师,通过比较器实现比较规则这个显示下面这一行

Map<Student,String> treeMap = new TreeMap<>(new StudentComparator());

出了问题,能帮我看一下是什么问题吗?

package 双例集合;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 元素自身实现比较规则   TreeMap可以对键进行排序
 */
public class TreeMapTest {
    public static void main(String[] args) {
        //实例化TreeMap
        Map<Users,String> map = new TreeMap<>();
        //创建User对象
        Users u1 = new Users("liu",18);
        Users u2 = new Users("admin",20);
        Users u3 = new Users("sxt",20);
        map.put(u1,"liu");
        map.put(u2,"admin");
        map.put(u3,"sxt");
        Set<Users> keys = map.keySet();
        for (Users key:keys){
            System.out.println(key+" "+map.get(key));
        }
        System.out.println("-----通过比较器实现比较规则-------");
        //java.lang.ClassCastException: 双例集合.Student cannot be cast to java.lang.Comparable
        //创建StudentComparator对象,然后交给TreeMap的构造方法中
        Map<Student,String> treeMap = new TreeMap<>(new StudentComparator());
        Student s1 = new Student("liu",18);
        Student s2 = new Student("admin",20);
        Student s3 = new Student("sxt",20);
        treeMap.put(s1,"liu");
        treeMap.put(s2,"admin");
        treeMap.put(s3,"sxt");
        Set<Student> keys2 = treeMap.keySet();
        for (Student key:keys2){
            System.out.println(key+" "+treeMap.get(key));
        }

    }
}

blob.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 30842楼
Python 全系列/第一阶段:Python入门/控制语句 30843楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 30844楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask数据库 30845楼
JAVA 全系列/第十八阶段:亿级高并发电商项目/亿级高并发电商项目(旧) 30846楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 30847楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 30849楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 30850楼

from fake_useragent import UserAgent
import ssl
import requests
from lxml import etree

from time import sleep

def get_html(url):
    '''
    :param url: 要爬取的地址
    :return: 返回html
    '''
    headers = {"User_Agent": UserAgent().random}
    resp = requests.get(url,headers=headers)

    #status_code   返回状态码
    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)
    # 解决验证CA
    # ssl._create_default_https_context = ssl._create_unverified_context
    list_url = ['https://www.qidian.com{}'.format(url)for url in e.xpath('//div[@class="book-img-box"]/a/@href')]
    return list_url

def parse_index(html):
    '''
    :param html: 传递一个有电影信息的html
    :return: 已经提取好的电影信息
    '''
    e = etree.HTML(html)
    name = e.xpath('//div/h1/span/a/text()')
    return name

def main():
    num = int(input("请输入要获取多少页:"))
    for page in range(num):
        url = 'https://www.qidian.com/all?&page={}'.format(page+1)
        list_html = get_html(url)
        list_url = parse_list(list_html)
        for url in list_url:
            info_html = get_html(url)
            move = parse_index(info_html)
            print(move)

if __name__ == '__main__':
    main()



老师,您帮我看一下,为什么我这个最后返回的是空列表啊,我debug看了一下是这个出问题了。返回了空值,但是我使用插件看了看没问题啊

name = e.xpath('//div/h1/span/a/text()')


Python 全系列/第十四阶段:Python 爬虫开发/爬虫反反爬- 30852楼
WEB前端全系列/第十九阶段:Vue2知识体系(旧)/Vue基础知识 30853楼
人工智能/第十四阶段:深度学习-目标检测YOLO(V8正在更新中)实战/YOLOv2详解 30854楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 30855楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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