You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python爬虫中如何将一个函数的输出作为另一个函数的参数使用?

代码修复说明

核心问题

你没有调用last_indeed_page()函数获取返回值,直接将未赋值的last_page变量作为参数传入了第二个函数,导致程序无法正常执行。

修复步骤

  • 先执行第一个函数拿到总页数返回值,再传入第二个函数,将原代码最后一行的调用逻辑替换为以下内容:
# 调用函数获取总页数
last_page = last_indeed_page()
# 传入参数执行爬取
extract_indeed_job(last_page)
  • 补充异常判断避免隐形报错
    你当前没有报错也没有输出,大概率是请求被网站反爬拦截,或者页面结构变化找不到分页元素,代码静默终止了,可修改第一个函数增加校验逻辑:
def last_indeed_page():
    indeed_info = requests.get(url, headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"})
    # 校验请求是否成功
    if indeed_info.status_code != 200:
        print(f"请求失败,状态码:{indeed_info.status_code}")
        return 1
    indeed_soup = BeautifulSoup(indeed_info.text, "html.parser")
    pagination = indeed_soup.find("ul", {"class": "pagination-list"})
    # 校验分页元素是否存在
    if not pagination:
        print("未找到分页组件,页面结构已变化或返回内容为空")
        return 1
    pages = pagination.find_all("li")
    a = []
    for page in pages[:-1]:
        page_text = page.get_text().strip()
        if page_text.isdigit():
            a.append(int(page_text))
    return a[-1] if a else 1
  • 检查前置依赖是否定义
    确认代码中的全局变量url、LIMIT,以及被调用的extract_job函数已经提前正确定义,避免执行时报错。

内容的提问来源于stack exchange,提问作者Seowoo Jang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 12:15:00