【问题标题】:How to get the output of a function to be the input of another function如何让一个函数的输出成为另一个函数的输入
【发布时间】:2021-01-13 16:11:13
【问题描述】:

我正在为食谱网站制作网络爬虫,我想获取食谱的链接,然后使用该链接获取成分。我能够做到这一点,但只能通过手动输入链接来获取食谱。有没有办法获取链接然后使用此链接查看成分。另外,我会就如何使这段代码变得更好提出任何建议!

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)

【问题讨论】:

    标签: python beautifulsoup web-crawler


    【解决方案1】:

    为此,请确保将您的输出设置为 return 而不是 print(要了解其中的区别,请尝试阅读这篇文章的最佳答案:What is the formal difference between "print" and "return"?

    然后您可以将函数的输出用作变量,或将输出直接放入下一个函数。 例如

    x = tradespider()
    

    newFunction(tradespider())
    

    【讨论】:

      【解决方案2】:

      您需要为从食谱中获得的每个链接调用 ingredient_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)
      

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

      【讨论】:

        【解决方案3】:

        老实说,我不确定我是否正确理解了您的要求,但如果我理解了,您可以使用以下内容:

        • 首先创建一个 URL 列表
        • 第二个创建一个可以处理 url 的函数
        • 最后创建一个工作于该列表的工作人员和平与和平

        .

        def first():
            URLs = [] 
            ...
            for link in soup.find_all('a', {'class':'feed-item analyt-internal-link-subunit'}): 
            URLs.append(link.get('href'))
            return URLs
        
        def second(url):
            source_code1= requests.get(url)
            new_text= source_code1.text
            soup1= BeautifulSoup(new_text, 'lxml')
            for ingredients in soup1.find_all("li", {"class": "ingredient xs-mb1 xs-mt0"}):
              return ingredients.text
          
        def third(URL_LIST):
            for URL in URL_LIST:
                 tmp = second(URL)
                 print(tmp)
        
        URL_LIST = first()
        third(URL_LIST)
        

        【讨论】:

          猜你喜欢
          • 2016-02-07
          • 2022-01-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-20
          • 1970-01-01
          • 2011-04-11
          • 1970-01-01
          相关资源
          最近更新 更多