简体   繁体   English

如何将一个 function 的 output 用于另一个 function?

[英]How do I use the output of one function for another function?

I was making the code to extract information from the website.我正在编写代码以从网站中提取信息。

And I wanted it to make for simple and try to organized in some function.我想让它变得简单,并尝试组织一些 function。

Now, I have a problem with using output of one function for another one.现在,我在将一个 function 的 output 用于另一个问题时遇到了问题。

def last_indeed_page():
    indeed_info = requests.get(url)
    indeed_soup = BeautifulSoup(indeed_info.text, "html.parser")
    pagination = indeed_soup.find("ul", {"class": "pagination-list"})
    pages = pagination.find_all("li")
    a = []
    for page in pages[:-1]:
        a.append(int(page.get_text()))
    last_page = a[-1]
    return last_page

I want to use this output last_page in next function.我想在下一个 function 中使用这个 output last_page

def extract_indeed_job(last_page):
    jobs = []
    for n in range(last_page):
        print(f"Indeed scraping page:{n+1}")
        result = requests.get(f"{url}&start={n*LIMIT}")
        result_soup = BeautifulSoup(result.text, "html.parser")
        results = result_soup.find_all("a", {"class": "tapItem"})
        for result in results:
            job = extract_job(result)
            jobs.append(job)
        print(jobs)

extract_indeed_job(last_page)

But, the output of this functions is nothing.但是,这个功能的output什么都不是。 It doesn't give me a error but just nothing.它没有给我一个错误,但什么也没有。
Which part do I have to change?我必须改变哪一部分?

You have 2 functions: last_indeed_page() , extract_indeed_job(last_page) ;您有 2 个功能: last_indeed_page()extract_indeed_job(last_page) and the output of last_indeed_page needs to be sent to extract_indeed_job .并且需要将 last_indeed_page 的last_indeed_page发送到extract_indeed_job

There are 2 ways to do that.有两种方法可以做到这一点。

  1. you can either create a variable and then call the function:您可以创建一个变量,然后调用 function:
last_page_variable = last_indeed_page()
extract_indeed_job(last_page_variable)
  1. you can directly call the last_indeed_page function inside the extract_indeed_job function so that the value returned by the last_indeed_page function is passed into extract_indeed_job你可以直接在extract_indeed_job function里面调用last_indeed_page ge function,这样last_indeed_page ZC1C425268E68385D1AB5074C1返回的值就会传入extract_indeed_job
extract_indeed_job(last_indeed_page())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个函数中使用一个函数收集的数据 - How do I use the data collected of one function in another function 如何将一个函数的输出用作另一个函数的参数 - How to use output for one function as parameter for another 如何使用函数的输出作为函数内另一个函数的输入 - How do I use the output of a function as the input for another function inside a function 如何从一个函数中获取一个值并在另一个函数中使用它? - How do I take a value from one function and use it in another? 如何在另一个 function 的输入中使用? - How do I use in input from one function in another? 如何使用一个 function 的 output 作为 Z23EEEB4347BDD26BDDFC6B7EE 中另一个 function 的输入 - how to use output of one function as input of another function in python 使用一个函数的输出并在另一个函数中取平均值 - Use output of one function and average it in another function 使用一个功能的输出作为另一个功能的输入 - Use output of one function as input of another function 如何将一个函数中生成的numpy.ndarray输出用作另一个函数的输入? - How can I use the numpy.ndarray output generated in one function as the input into another function? 如何在同一类的另一个函数中使用一个函数中的一个对象 - How do I use one object from one function in another function in the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM