Python语言,函数A调用函数B,B先return一个信息给A,接着调用函数C,请问如何实现
def C():
print("C")
def myfunc():
yield 123
C()
yield 456
def A():
gen = myfunc()
print(gen.__next__())
print("然后继续执行")
gen.__next__()
A()
def functionA():
# 调用函数B并接收返回的信息
info = functionB()
# 在这里可以使用返回的信息进行处理
print("Received info from functionB:", info)
# 继续执行后续代码
functionC()
def functionB():
# 返回一个信息给调用它的函数
return "Hello from functionB!"
def functionC():
print("This is functionC.")
# 调用函数A
functionA()
输出
Received info from functionB: Hello from functionB!
This is functionC.
# -*- coding:utf-8 -*-
import re
from bs4 import BeautifulSoup
import urllib
import pandas as pd
class Spider():
'''
Description:
Spider program to crawl data from bilibili.com hot search rank list
Attributes:
None
'''
def __init__(self):
self.url = 'https://www.bilibili.com/ranking?spm_id_from=333.851.b_7072696d61727950616765546162.3'
# regular matching pattern
self.pattern = re.compile(r'<a class="title" href="(.*?)" target="_blank">(.*?)</a>')
self.headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"}
'''
Description:
crawl page from the given URL
Args:
url: the URL of page need to get
Returns:
page of the given URL
'''
def crawl(self):
self.request = urllib.request.Request(headers = self.headers, url = self.url)
self.response = urllib.request.urlopen(self.request)
page = self.response.read().decode('utf-8')
return page
'''
Description:
extract data from the given page by bs4 and re library, return the list of data
Args:
None
Returns:
list of data extract from given page
'''
def extract(self):
page = self.crawl()
beautifulSoup = BeautifulSoup(page, 'html.parser')
results = []
for frame in beautifulSoup.find_all('div', class_ = 'content'):
frame = str(frame)
result = re.findall(self.pattern, frame)[0]
results.append(result)
return results
if __name__ == "__main__":
spider = Spider()
results = spider.extract()
print(pd.DataFrame(results))
在函数B中,可以通过input()函数让用户输入信息,并保存在一个变量中。然后再返回这个变量给函数A。具体实现可以参考如下代码:
def functionB():
user_input = input("Please enter information:")
return user_input
def functionA():
result_from_B = functionB()
#continue to execute the code in functionA
return result_from_B