无法让进度条在 python rich 中工作

问题描述 投票:0回答:3

我正在尝试在我的代码中添加一个丰富的进度条。但是,当代码运行时,该栏仅在完成后更新为 100%。我可以帮忙吗?我的代码:

theme = Theme({'success': 'bold green',
              'error': 'bold red', 'enter': 'bold blue'})
console = Console(theme=(theme))
for i in track(range(1), description='Scraping'):
    global pfp
    global target_id
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    driver = webdriver.Chrome(options=chrome_options)
    begining_of_url = "https://lookup.guru/"
    whole_url = begining_of_url + str(target_id)
    driver.get(whole_url)
    wait = WebDriverWait(driver, 10)
    wait.until(EC.visibility_of_element_located((By.XPATH, "//img")))
    images = driver.find_elements_by_tag_name('img')
    for image in images:
        global pfp
        pfp = (image.get_attribute('src'))
        break
    if pfp == "a":
        console.print("User not found \n", style='error')
        userInput()
    img_data = requests.get(pfp).content
    with open('pfpimage.png', 'wb') as handler:
        handler.write(img_data)
    filePath = "pfpimage.png"
    searchUrl = 'https://yandex.com/images/search'
    files = {'upfile': ('blob', open(filePath, 'rb'), 'image/jpeg')}
    params = {'rpt': 'imageview', 'format': 'json',
              'request': '{"blocks":[{"block":"b-page_type_search-by-image__link"}]}'}
    response = requests.post(searchUrl, params=params, files=files)
    query_string = json.loads(response.content)[
                              'blocks'][0]['params']['url']
    img_search_url = searchUrl + '?' + query_string
    webbrowser.open(whole_url)
    webbrowser.open(img_search_url)
    console.print("Done!", style='success')

编辑: 为了更清楚起见,我希望进度条在遍历代码的每个部分时进行更新。只有一个 url 可供抓取。例如,它将从 0% 开始,在

global pfp
之后,条形将更改为 x%

感谢您的帮助:)

python progress-bar rich
3个回答
2
投票

为了显示进度条,Rich 需要知道涉及多少个步骤以及何时完成步骤。

track
函数可以从序列中自动获取此信息。您在示例中使用了这个,但您的序列只有一个元素,因此您可以一步从 0 到 100%。

如果您想跟踪某件事的进度,您需要一个定义要完成的工作的序列。例如,如果您有一个要抓取的网址列表,您可能会执行以下操作:

from rich.progress import track
SCRAPE_URLS = ["https://example.org", "https://google.org", ...]
for url in track(SCRAPE_URLS):
    scrape(url)

每个 URL 的进度条都会前进。


0
投票

问题是,通过使用

for i in track(range(1), description='Scraping'):
,只有当循环完成时,条才会达到 100%。通过更改
range()
值将使代码循环并更新栏。为了解决这个问题,我使用了另一个名为 Progress
rich
模块。

通过导入

Progress
,然后修改丰富的文档上的代码,我得到了:

from rich.progress import Progress
import time

with Progress() as progress:

    task1 = progress.add_task("[red]Scraping", total=100)

    while not progress.finished:
        progress.update(task1, advance=0.5)
        time.sleep(0.5)

本质上:

  • task1 = progress.add_task("[red]Scraping", total=100)
    创建一个条形,其最大值为 100
  • while not progress.finished:
    下缩进的代码将循环,直到栏位于 100%
  • progress.update(task1, advance=0.5)
    ,条形的总数将增加 0.5 的值。

因此,对于我的具体示例,我的最终结果代码是:

theme = Theme({'success': 'bold green',
                  'error': 'bold red', 'enter': 'bold blue'})
console = Console(theme=(theme))
bartotal = 100

with Progress() as progress:
    task1 = progress.add_task("[magenta bold]Scraping...", total=bartotal)
    while not progress.finished:
                console.print("\nDeclaring global variables", style='success')
                global pfp
                progress.update(task1, advance=4)
                global target_id
                progress.update(task1, advance=4)
                console.print("\nSetting up Chrome driver", style='success')
                chrome_options = Options()
                progress.update(task1, advance=4)
                chrome_options.add_argument("--headless")
                progress.update(task1, advance=4)
                driver = webdriver.Chrome(options=chrome_options)
                progress.update(task1, advance=4)
                console.print("\nCreating url for lookup.guru",
                              style='success')
                begining_of_url = "https://lookup.guru/"
                progress.update(task1, advance=4)
                whole_url = begining_of_url + str(target_id)
                progress.update(task1, advance=4)
                driver.get(whole_url)
                progress.update(task1, advance=4)
                console.print(
                    "\nWaiting up to 10 seconds for lookup.guru to load", style='success')
                wait = WebDriverWait(driver, 10)
                progress.update(task1, advance=4)
                wait.until(EC.visibility_of_element_located(
                    (By.XPATH, "//img")))
                progress.update(task1, advance=4)
                console.print("\nScraping images", style='success')
                images = driver.find_elements_by_tag_name('img')
                progress.update(task1, advance=4)
                for image in images:
                    global pfp
                    pfp = (image.get_attribute('src'))
                    break
                progress.update(task1, advance=4)
                if pfp == "a":
                    console.print("User not found \n", style='error')
                    userInput()
                progress.update(task1, advance=4)
                console.print(
                    "\nDownloading image to current directory", style='success')
                img_data = requests.get(pfp).content
                progress.update(task1, advance=4)
                with open('pfpimage.png', 'wb') as handler:
                    handler.write(img_data)
                progress.update(task1, advance=4)
                filePath = "pfpimage.png"
                progress.update(task1, advance=4)
                console.print("\nUploading to yandex.com", style='success')
                searchUrl = 'https://yandex.com/images/search'
                progress.update(task1, advance=4)
                files = {'upfile': ('blob', open(
                    filePath, 'rb'), 'image/jpeg')}
                progress.update(task1, advance=4)
                params = {'rpt': 'imageview', 'format': 'json',
                          'request': '{"blocks":[{"block":"b-page_type_search-by-image__link"}]}'}
                progress.update(task1, advance=4)
                response = requests.post(searchUrl, params=params, files=files)
                progress.update(task1, advance=4)
                query_string = json.loads(response.content)[
                                          'blocks'][0]['params']['url']
                progress.update(task1, advance=4)
                img_search_url = searchUrl + '?' + query_string
                progress.update(task1, advance=4)
                console.print("\nOpening lookup.guru", style='success')
                webbrowser.open(whole_url)
                progress.update(task1, advance=4)
                console.print("\nOpening yandex images", style='success')
                webbrowser.open(img_search_url)
                progress.update(task1, advance=4)
                console.print("\nDone!", style='success')
                progress.update(task1, advance=4)

0
投票

为了使

rich
软件包正常工作,您需要检查
Emulate terminal in output console
,您可以在
Run
菜单(顶部)中找到它,然后是
Edit configurations
,然后是
Modify options

Emulate terminal in output console

我遇到了同样的问题(该栏仅在过程完成后才出现),这解决了它。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.