【问题标题】:how I can get the real-time progress bar in BeautifulSoup python?如何在 BeautifulSoup python 中获取实时进度条?
【发布时间】:2021-06-13 19:18:08
【问题描述】:

我有以下代码,该代码从 Redbubble 等网站抓取了一些数据。有时我会刮很多数据,想知道代码中的实时进度……我尝试了进度条模块,但没有得到我想要的……

import requests
from bs4 import BeautifulSoup

re = requests.get('https://www.redbubble.com/i/iphone-case/What-A-Time-To-Be-Alive-by-DinoMike/36490886.RIOBD')
        
src = re.content

soup = BeautifulSoup(src, "html.parser")

tags = soup.find_all("span", {"class" : "styles__children--21o3C"})

print(tags)

【问题讨论】:

  • 从您的代码中可以看出,您只执行一个请求。是不是需要超过半秒的时间?
  • 这个请求只是一个例子

标签: python python-3.x progress-bar


【解决方案1】:

如果您有多个页面要请求,这里有一个很酷的库,tqdm,它会显示一个进度条。

import requests
from bs4 import BeautifulSoup
from tqdm import tqdm

# set of target URLs
urls = [
    "https://www.redbubble.com/i/iphone-case/What-A-Time-To-Be-Alive-by-DinoMike/36490886.RIOBD",
    ...
]
set_tags = []
# go through the list
for url in tqdm(urls):
    # get request
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    tags = soup.find_all("span", {"class": "styles__children--21o3C"})
    set_tags.append(tags)

【讨论】:

  • 我能以整数形式获得进度吗(15%、30%、40% 等)?
  • tqdm 实时显示进度。
  • 我知道,但是否有能力获取整数的实时进度并将这些整数附加到列表中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
  • 2010-11-18
  • 2010-11-11
  • 1970-01-01
  • 2011-11-29
相关资源
最近更新 更多