简体   繁体   中英

Save generated image (graph) from url to a file using python

I am attempting to save a generated graph from a url to an image file automatically using Python 3. The link itself is not a .png but when I right click on the image in a browser it can be saved as a .png picture. The remainder of my coding is already in python as majority is automated trend analysis, so I would like to be able to do this step in python as well. The coding uses an input and changes the dates within the link to download certain graphs. Here is the simplified part of the code that I am struggling with. The test case works fine, but the actual file I wish to save does not.

import os
from urllib.request import urlretrieve

Link_Test = "http://www.catster.com/wp-content/uploads/2017/06/small-kitten-meowing.jpg"
Link_Actual = "https://waterdata.usgs.gov/nwis/dv/?ts_id=52441,52442,52443&format=img_stats&site_no=405902087141501&set_arithscale_y=on&begin_date=20161001&end_date=20170930"

urlretrieve(Link_Test, "Test.jpg") #This saves the test picture correctly
urlretrieve(Link_Actual, "JasperCounty.png") #The saved file is unreadable

I have tried other methods for downloading pictures and they all work for the test case, but I cannot find a way to download the generated graph as a picture. Thanks for any help!!!

It probably makes more sense to search the page of each link for images and save those images rather that relying on the URL alone

import re
import requests
from bs4 import BeautifulSoup

site = 'https://waterdata.usgs.gov/nwis/dv/?ts_id=52441,52442,52443&format=img_stats&site_no=405902087141501&set_arithscale_y=on&begin_date=20161001&end_date=20170930'

response = requests.get(site)

soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all('img')

urls = [img['src'] for img in img_tags]


for url in urls:
    filename = re.search(r'/([\w_-]+[.](jpg|gif|png))$', url)
    with open(filename.group(1), 'wb') as f:
        response = requests.get(url)
        f.write(response.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