Search code examples
pythonurllib2urllib3

How to get image from dynamic url using urllib2?


I have generated a url from product code like,

code: 2555-525
url : www.example.com/2555-525.png

But when fetching a url, it might be a different name format on server,like

www.example.com/2555-525.png
www.example.com/2555-525_TEXT.png
www.example.com/2555-525_TEXT_TEXT.png

Sample code,

urllib2.urlopen(URL).read()

could we pass the url like www.example.com/2555-525*.png ?


Solution

  • Using wildcards in URLs is useless in most cases because

    • the interpretation of the part of the URL after http://www.example.com/ is totally up to the server - so http://www.example.com/2555-525*.png might have a meaning to the server but but propably has not

    • normally (exceptions like WebDAV exist) there is no way of listing ressources in a collection or existing URLs in general apart from trying them one-by-one (which is unpractical) or scraping a known site for URLs (which might be incomplete)

    For finding and downloading URLs automatically you can use a Web Crawler or Spider.