Does Python download all files from an internet address?

I want to download all files from a web page, in fact all image files. I found that the urllib module is what I need. There seems to be a way to upload the file if you know the file name, but I do not.

urllib.urlretrieve('http://www.example.com/page', 'myfile.jpg')

Is there a way to download all the files from the page and possibly return the list?

+5
source share
1 answer

, BeautifulSoup - script URL- URL- , src img, jpg png:

import sys, urllib, re, urlparse
from BeautifulSoup import BeautifulSoup

if not len(sys.argv) == 2:
    print >> sys.stderr, "Usage: %s <URL>" % (sys.argv[0],)
    sys.exit(1)

url = sys.argv[1]

f = urllib.urlopen(url)
soup = BeautifulSoup(f)
for i in soup.findAll('img', attrs={'src': re.compile('(?i)(jpg|png)$')}):
    full_url = urlparse.urljoin(url, i['src'])
    print "image URL: ", full_url

urllib.urlretrieve , full_url, , , .

+6

All Articles