python - 如何让一个函数的输出成为另一个函数的输入
问题描述
我正在为食谱网站制作网络爬虫,我想获取食谱的链接,然后使用该链接获取成分。我能够做到这一点,但只能通过手动输入链接来获取食谱。有没有办法获取链接然后使用此链接查看成分。此外,我将就如何使此代码更好地提出任何建议!
def trade_spider():
url= 'https://tasty.co/topic/best-vegetarian'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'lxml')
for link in soup.find_all('a', {'class':'feed-item analyt-internal-link-subunit'}):
test = link.get('href')
print(test)
def ingredient_spider():
url1= 'https://tasty.co/recipe/peanut-butter-keto-cookies'
source_code1= requests.get(url1)
new_text= source_code1.text
soup1= BeautifulSoup(new_text, 'lxml')
for ingredients in soup1.find_all("li", {"class": "ingredient xs-mb1 xs-mt0"}):
print(ingredients.text)
解决方案
为此,请确保将您的输出设置为return
而不是print
(要了解差异,请尝试阅读此帖子的最佳答案:“打印”和“返回”之间的正式区别是什么?)
然后,您可以将函数的输出用作变量,或将输出直接放入下一个函数。例如
x = tradespider()
或者
newFunction(tradespider())
推荐阅读
- flutter - 颤振未处理的异常:类型“(动态)=>动态”不是“测试”类型“(动态)=>布尔”的子类型
- sql - SOQL/SQL:左外连接查询的结果不正确
- c# - 通过DataSet.Clear(),DataTable 和DataColumn 和DataRow 也被删除了?
- go - 具有工作池的基准功能,没有工作人员但速度更快,用于测试结果
- visual-studio - 初始化模板 Visual Studio 2019 时遇到错误
- python - 如何结束周长计算循环
- android - 如何在 Flutter 的 WebView 中使用 post 方法
- android - 通过我的 Flutter 应用与第三方应用交互
- blazor - 托管的 Blazor wasm 会在它们是处理服务器端页面的控制器操作时不断加载 Blazor 应用程序
- python - 更改 tensorflow 保存模型的 dtype 策略