Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使一个函数的输出成为另一个函数的输入_Python_Beautifulsoup_Web Crawler - Fatal编程技术网

Python 如何使一个函数的输出成为另一个函数的输入

Python 如何使一个函数的输出成为另一个函数的输入,python,beautifulsoup,web-crawler,Python,Beautifulsoup,Web Crawler,我正在为一个食谱网站制作一个网络爬虫,我想获取一个食谱的链接,然后使用该链接获取配料。我可以做到这一点,但只有手动输入链接才能获得配方。有没有办法获得链接,然后使用此链接查看配料。此外,我将采取任何建议,如何使这段代码更好 def trade_spider(): url= 'https://tasty.co/topic/best-vegetarian' source_code = requests.get(url) plain_text = source_code.text soup =

我正在为一个食谱网站制作一个网络爬虫,我想获取一个食谱的链接,然后使用该链接获取配料。我可以做到这一点,但只有手动输入链接才能获得配方。有没有办法获得链接,然后使用此链接查看配料。此外,我将采取任何建议,如何使这段代码更好

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()


老实说,我不确定我是否正确理解了你的要求,但如果我理解了,你可以这样说:

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')
           ingredient_spider(test)

def ingredient_spider(url):
 source_code1= requests.get(url) #receive url from trade_spider function
 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)
  • 首先创建一个URL列表
  • 第二,创建一个可以使用url的函数
  • 最后创建一个工作人员,该工作人员逐个和平地完成该列表


您需要为从配方中获得的每个链接调用配料蜘蛛函数。 使用您的示例,它将如下所示:

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')
           ingredient_spider(test)

def ingredient_spider(url):
 source_code1= requests.get(url) #receive url from trade_spider function
 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)

对于从test=link.get('href')中获得的每个链接,您可以调用函数component_spider(),将测试变量作为参数发送。

非常有效,谢谢!
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')
           ingredient_spider(test)

def ingredient_spider(url):
 source_code1= requests.get(url) #receive url from trade_spider function
 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)