无法让进度条在 python rich 中工作
Can't get progress bar to work in python rich
我正在尝试在我的代码中添加一个带有 rich 的进度条。但是,虽然代码为 运行,但该条仅在完成后更新为 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%
感谢您的帮助:)
为了显示进度条,Rich 需要知道涉及多少个步骤以及完成一个步骤的时间。 track
函数可以自动从序列中获取此信息。您在您的示例中使用了它,但您的序列只有一个元素,因此您只需一步即可从 0 到 100%。
如果您想跟踪某件事的进度,您需要一个定义要完成的工作的顺序。例如,如果你有一个要抓取的 url 列表,你可以这样做:
from rich.progress import track
SCRAPE_URLS = ["https://example.org", "https://google.org", ...]
for url in track(SCRAPE_URLS):
scrape(url)
进度条每URL.
前进
问题在于,通过使用 for i in track(range(1), description='Scraping'):
,当循环结束时,条形图只会达到 100%。通过更改 range()
值将使代码循环并更新栏。为了解决这个问题,我使用了另一个名为 Progress
.
的 rich 模块
通过导入 Progress
然后修改 Rich Documentation 上的代码,我得到:
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)
我正在尝试在我的代码中添加一个带有 rich 的进度条。但是,虽然代码为 运行,但该条仅在完成后更新为 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%
感谢您的帮助:)
为了显示进度条,Rich 需要知道涉及多少个步骤以及完成一个步骤的时间。 track
函数可以自动从序列中获取此信息。您在您的示例中使用了它,但您的序列只有一个元素,因此您只需一步即可从 0 到 100%。
如果您想跟踪某件事的进度,您需要一个定义要完成的工作的顺序。例如,如果你有一个要抓取的 url 列表,你可以这样做:
from rich.progress import track
SCRAPE_URLS = ["https://example.org", "https://google.org", ...]
for url in track(SCRAPE_URLS):
scrape(url)
进度条每URL.
前进问题在于,通过使用 for i in track(range(1), description='Scraping'):
,当循环结束时,条形图只会达到 100%。通过更改 range()
值将使代码循环并更新栏。为了解决这个问题,我使用了另一个名为 Progress
.
通过导入 Progress
然后修改 Rich Documentation 上的代码,我得到:
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)