简体   繁体   中英

Requests Not Creating Images Python

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM