[英]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.有两种方法可以做到这一点。
last_page_variable = last_indeed_page()
extract_indeed_job(last_page_variable)
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.