[英]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.