会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132360个问题
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 586楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用 587楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 588楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 589楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 590楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 591楼


xiaoshuo.zip




老师,这个是源代码

Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 592楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 593楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 594楼

import requests
from fake_useragent import UserAgent
from bs4 import BeautifulSoup
from time import sleep

def get_list():
    num = int(input("请输入要获取多少页数据:"))
    for i in range(num):
        url = "https://maoyan.com/films?showType=3&offset={i*30}"
        headers = {'User-Agent':UserAgent().chrome}
        resp = requests.get(url,headers=headers)
        soup = BeautifulSoup(resp.text,'lxml')
        all_a = soup.select('div > a[data-act="movies-click"]')
        print(all_a)
        return [a.get('href') for a in all_a]

# 格式化演员集合,去重
def format_actors(a_list):
    actor_set = set()
    for a in a_list:
        actor_set.add(a.text.strip())       # 需要a标签里的文本,去除空格
    return actor_set

def start():
    all_href = get_list()
    for a in all_href:
        sleep(2)
        url = f"https://maoyan.com{a}"
        headers = {'User-Agent':UserAgent().chrome}
        resp = requests.get(url,headers=headers)
        soup = BeautifulSoup(resp.text,'lxml')

        name = soup.select('h1.name')[0].text.strip()
        types = soup.select('li.ellipsis')[0].text.strip()
        actors_m = soup.select('li.celebrity.actor > div > a')
        actors = format_actors(actors_m)
        print(f'电影名:{name}  电影类型:{types}   演员:{actors}')

if __name__ == '__main__':
    start()

老师,帮我看看这是为什么?我的输出没有结果!

图片.png

Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 598楼
Python 全系列/第十五阶段:Python 爬虫开发/动态数据抓取 599楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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