简体   繁体   English

是否有一种解决方法可以防止特定方法将内容打印到控制台?

[英]Is there a workaround to prevent a particular method from printing things to the console?

when I'm scraping details for specific apps in the App Store, it prints out a URL to the console even when there isn't an print statement.当我在 App Store 中抓取特定应用程序的详细信息时,即使没有打印语句,它也会将 URL 打印到控制台。 Here's my experimental code snippet and the current output.这是我的实验代码片段和当前的 output。

!pip install itunes-app-scraper-dmi

from itunes_app_scraper.scraper import AppStoreScraper

print(" ")
bundleID = "com.marmalade.monopoly"
appDetails = AppStoreScraper().get_app_details(bundleID, country="us")
version = appDetails['version']
url = appDetails['trackViewUrl'][:-5]

print("\nVersion:", version)
print("App URL:", url)

And it outputs the following.它输出以下内容。

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: itunes-app-scraper-dmi in /usr/local/lib/python3.7/dist-packages (0.9.4)
 
https://itunes.apple.com/lookup?bundleId=com.marmalade.monopoly&country=us&entity=software

Version: 1.7.15
App URL: https://apps.apple.com/us/app/monopoly-classic-board-game/id1477966166

I plan to merge this into my main code which will involve iterating through a larger list of apps, and I'm not interested in the URL that gets printed to the console.我计划将其合并到我的主要代码中,这将涉及遍历更大的应用程序列表,并且我对打印到控制台的 URL 不感兴趣。 I'm only interested in grabbing couple pieces of info for each app and storing them into variables for further processing.我只对为每个应用程序获取几条信息并将它们存储到变量中以供进一步处理感兴趣。

In the code example, it prints this URL to the console with or without independent print statements.在代码示例中,无论是否使用独立的打印语句,它都会将此 URL 打印到控制台。 ( https://itunes.apple.com/lookup?bundleId=com.marmalade.monopoly&country=us&entity=software ) https://itunes.apple.com/lookup?bundleId=com.marmalade.monopoly&country=us&entity=software

I looked at the get_app_details() method in (2) below and couldn't find where it prints the URL.我查看了下面 (2) 中的get_app_details()方法,找不到打印 URL 的位置。 In here, I'm looking at Line 177 and anything after, where it puts together the URL based on the given bundle ID.在这里,我正在查看第 177 行和之后的任何内容,它根据给定的捆绑 ID 将 URL 放在一起。 Line 180 seems to be storing the result of requests.get(url).json() into the variable result .第 180 行似乎将requests.get(url).json()的结果存储到变量result中。 Could this sub-method be responsible for printing the URL without its own print statement?这个子方法可以在没有自己的打印语句的情况下负责打印 URL 吗?

If so, is there a workaround to prevent the URL from being printed to the console?如果是这样,是否有防止 URL 打印到控制台的解决方法?

Documentation文档

Quick update, I managed to find a solution for this.快速更新,我设法找到了解决方案。 I used the following code snippets throughout my main code, when I didn't want the method to print the URL.当我不希望该方法打印 URL 时,我在我的主代码中使用了以下代码片段。

from IPython.utils import io

with io.capture_output() as captured:
    print("Output Text")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 阻止Paramiko打印到控制台 - Prevent Paramiko from printing to console 防止函数在 Python 中的批处理控制台中打印 - To prevent a function from printing in the batch console in Python 防止 Python 记录器打印到控制台 - Prevent Python logger from printing to console 防止OpenCV函数CreateVideoWriter在Python中打印到控制台 - Prevent OpenCV function CreateVideoWriter from printing to console in Python 从列表中打印 2 件事两次,然后打印接下来的 2 件事两次(循环) - Printing 2 things from a list twice then printing next 2 things twice (loop) 停止 Tensorflow 打印到控制台 - Stop Tensorflow from printing to the console 防止python打印换行符 - Prevent python from printing newline 如何在从 class 方法打印时打印列表(不是整个列表)中的“特定”元素,不带括号和引号? - How to print a 'particular' element in a list (not entire list), without brackets and quotes, while printing from class method? 在打印到控制台以使事情看起来更好时,是否有一堆换行符? - Is it common to have a bunch of newlines when printing to the console to make things look nicer? 如何从 chrome 控制台获取内容到 python 列表中? - how can i get things from the chrome console into a python list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM