0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.