请求不创建图像Python

发布于 2025-02-11 08:20:06 字数 1021 浏览 0 评论 0原文

我想下载图像。但是由于某种原因,代码没有错误执行,但没有创建任何图像。
我正在使用请求beautifulsoup。我的IDE是VS代码

HEADERS = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246"}
def getData(link):
  URL = link
  request = requests.get(url=URL, headers=HEADERS)
  result = BeautifulSoup(request.content, "lxml")
  return result
address = "https://bisesargodha.edu.pk/content/ViewSeqImges.aspx?SubjectId=760&SeqNameAnsSubj=Sequence1"
response = getData(address)
table = response.find('table', attrs = {'id':'ContentPlaceHolder1_Table1'})
imgs = table.findAll('img')
imgNum = 1
for img in imgs:
    image_url = f"https://bisesargodha.edu.pk/content/{img['src']}"
    image_save = f"img-{imgNum}.jpg"
    pull_image = requests.get(image_url, headers=HEADERS)
    pull_image_contant = pull_image.content
    with open(image_save, "wb+") as myfile:
        myfile.write(pull_image_contant)
    imgNum = imgNum + 1

I want to download images. But for some reason the code execute without errors but it's not creating any images.
I'm using Requests and BeautifulSoup. My IDE is VS Code

HEADERS = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246"}
def getData(link):
  URL = link
  request = requests.get(url=URL, headers=HEADERS)
  result = BeautifulSoup(request.content, "lxml")
  return result
address = "https://bisesargodha.edu.pk/content/ViewSeqImges.aspx?SubjectId=760&SeqNameAnsSubj=Sequence1"
response = getData(address)
table = response.find('table', attrs = {'id':'ContentPlaceHolder1_Table1'})
imgs = table.findAll('img')
imgNum = 1
for img in imgs:
    image_url = f"https://bisesargodha.edu.pk/content/{img['src']}"
    image_save = f"img-{imgNum}.jpg"
    pull_image = requests.get(image_url, headers=HEADERS)
    pull_image_contant = pull_image.content
    with open(image_save, "wb+") as myfile:
        myfile.write(pull_image_contant)
    imgNum = imgNum + 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2025-02-18 08:20:06

您需要获取并消耗响应作为流,请尝试这样的事情:

img_num = 1
for img in imgs:
    image_url = f"https://bisesargodha.edu.pk/content/{img['src']}"
    image_save = f"img-{img_num}.jpg"

    with requests.get(image_url, headers=HEADERS, stream=True) as resp:
        resp.raise_for_status() # do some additional error handling if necessary
        with open(image_save, "wb") as image_file:
            for chunk in r.iter_content(chunk_size=8192): 
                image_file.write(chunk)

    img_num = img_num + 1

如果问题仍然存在,则可能会仔细检查您正在构造的图像URL,并确保它们真正指向正确的内容。

You need to fetch and consume your response as a stream, try something like this:

img_num = 1
for img in imgs:
    image_url = f"https://bisesargodha.edu.pk/content/{img['src']}"
    image_save = f"img-{img_num}.jpg"

    with requests.get(image_url, headers=HEADERS, stream=True) as resp:
        resp.raise_for_status() # do some additional error handling if necessary
        with open(image_save, "wb") as image_file:
            for chunk in r.iter_content(chunk_size=8192): 
                image_file.write(chunk)

    img_num = img_num + 1

If the issue still persists then maybe double check the image urls you are constructing and make sure they are really pointing to the right content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文