title: python-download all images from the webpages and into a file
body: <p>I would like to download all images from this webpage, but my code are not workable.
  How can I revise that given this script.</p>
  <pre><code>import requests,os
  from bs4 import BeautifulSoup
  from urllib.request import urlopen
  
  html=requests.get('https://www.dreamstime.com/free-results.php?securitycheck=afbb79db0e7e374867295876228b135a&amp;firstvalue=&amp;lastsearchvalue=&amp;srh_field=doges&amp;searchby=doges&amp;s_free=y&amp;s_cc0=y',headers={&quot;User-Agent&quot;: &quot;XY&quot;})
  html.encoding='utf-8'
  
  sp=BeautifulSoup(html.text,'html.parser')
  images_dir=&quot;images/&quot;
  if not os.path.exists(images_dir):
      os.mkdir(images_dir)
  
  all_links=sp.find_all(['a','img'])
  for link in all_links:
      src=link.get(&quot;src&quot;)
      href=link.get(&quot;href&quot;)
      attrs=[src,href]
      for attr in attrs:
          if attr != None and ('.jpg' in attr or '.png' in attr):
              full_path=attr
              filename=full_path.split('/')[-1]
              print(full_path)
              
              try:
                  image=urlopen(full_path)
                  f=open(os.path.join(images_dir,filename),'wb')
                  f.write(image.read())
                  f.close()
              except:
                  print(&quot;{} fail&quot;.format(filename))
  </code></pre>