Import urllib2 from bS4 import BeautifulSoup import socket baseurl = “dbmeizenter” def user_agent(url): Req_header = {‘ the user-agent ‘:’ Mozilla / 5.0 (Windows; U; Windows NT 6.1; en-US; Rv :1.9.1.6) Gecko/20091201 Firefox/3.5.6′} req_timeout = 20 try: req = urllib2.Request(url,None,req_header) page = urllib2.urlopen(req,None,req_timeout) html = page except urllib2.URLError as e: print e.message except socket.timeout as e: user_agent(url) return html def page_loop(pageid): url = baseurl+’? p=%s’%pageid print url page = user_agent(url) soup = BeautifulSoup(page) total_img = 0 img = soup.find_all([‘img’]) for myimg in img: link = myimg.get(‘src’) total_img += 1 print link # content2 = urllib2.urlopen(link).read() content2 = User_agent (link).read() # user_agent(link).read() #D:\myimg #D:\myimg #D:\myimg open(u’D:\myimg’+’/’+link[-11:],’wb’) as code: code.write(content2) print total_img return total_img page_start = 0 page_stop = 4 total = 0 for i in Range (page_start,page_stop): total+=page_loop(I) print total #total