会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132469个问题



    
    Title
    
        {
            : ;
        }
        .,.,.{
            : %;
        }
        .{
            : ;
        }
        .{
            : ;
            : ;
            :;
            : ;
            : ;
        }
        .{
            : ;
        }
        .{
            : ;
        }
        .>{
            : ;
            : ;
            : ;
            : ;
            :;
            : ;

        }
        .,.,.,.{
            : ;
        }
        .>{
            : ;
            : ;
            : ;
            : ;
            : ;
        }
        .{
           : ;
            : ;
        }
        .{
            : ;
            : ;
        }
        .{
            : ;
            : ;
        }
        .{
            : ;
        }
        .{
            : ;
            : ;
            : ;
        }
        .{
            : ;
            : ;
        }
        .{
            : ;
            : ;

        }
        .{
            : ;
            : ;

        }
        .{
            : ;
            : ;

        }
        .{
            : ;
        }

    



    
        
            
                
                    18
                    200 | 
                    3999
                    
                
            
            
                
                    18 Pro
                    300 | 
                    4999
                    
                
            

        
        
            
                
                    
                    17AG
                    865 | 120 Hz
                    3699

                
            
            
            
                
                17
                865 | 120 Hz
                3699
            
            
            
                
                    
                    17 Pro
                    1200+12
                    4299
                
            
            
                
                    
                    17 Pro
                    1200+12
                    4299

blob.png

WEB前端全系列/第一阶段:HTML5+CSS3模块/初识CSS 21031楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 21033楼
WEB前端全系列/第十九阶段:Vue2知识体系(旧)/Vue基础知识 21034楼

老师,我这个打印不出来内容是为啥,看下面的评论说没联网,可是我网络也没问题啊

image.png代码:

from fake_useragent import UserAgent
import requests
from lxml import etree
from time import sleep


def get_html(url):
    '''
    :param url: 要爬取的地址
    :return: 返回html
    '''
    headers = {"User-Agent": UserAgent().chrome}
    resp = requests.get(url, headers=headers)
    # sleep(2)
    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 = ['http://maoyan.com{}'.format(url) for url in e.xpath('//dd/div[@class="movie-item film-channel"]/a/@href')]
    return list_url


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


def format_data(actors):
    actor_set = set()
    for actor in actors:
        actor_set.add(actor.strip())
    return actor_set


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


if __name__ == '__main__':
    main()


Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 21036楼
Python 全系列/第一阶段:Python入门/函数和内存分析 21043楼
Python 全系列/第十五阶段:Python 爬虫开发/动态数据抓取 21045楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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