python - Save To Folder In List On Each Loop -
i have code below downloads files on each loop. what on each loop should extract files corresponding folder. whats happening saving files in each folder , overwriting files same names.
any appreciated.
import os import requests import zipfile, stringio bs4 import beautifulsoup # here add login details submitted login form. payloads = [ {'username': '1111','password': '1111','option': 'login'}, {'username': '2222','password': '2222','option': 'login'}, {'username': '3333','password': '3333','option': 'login'}, {'username': '4444','password': '4444','option': 'login'}, ] folders = [r"c:\temp\1111", r"c:\temp\2222", r"c:\temp\3333", r"c:\temp\4444"] #possibly need headers later. headers = {'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_12_5) applewebkit/537.36 (khtml, gecko) chrome/59.0.3071.115 safari/537.36'} base_url = "https://service.rl360.com/scripts/customer.cgi/sc/servicing/" # use 'with' ensure session context closed after use. payload in payloads: requests.session() s: p = s.post('https://service.rl360.com/scripts/customer.cgi?option=login', data=payload) # download page scrape. r = s.get('https://service.rl360.com/scripts/customer.cgi/sc/servicing/downloads.php?folder=datadownloads&sortfield=reportdate&sortorder=ascending', stream=true) content = r.text soup = beautifulsoup(content, 'lxml') #now recent download url. download_url = soup.find_all("a", {'class':'tabletd'})[-1]['href'] #now join base url download url. download_docs = s.get(base_url + download_url, stream=true) print "checking content" content_type = download_docs.headers['content-type'] print content_type print "checking filename" content_name = download_docs.headers['content-disposition'] print content_name print "checking download size" content_size = download_docs.headers['content-length'] print content_size #this extract , download specified xml files. z = zipfile.zipfile(stringio.stringio(download_docs.content)) print "---------------------------------" print "downloading........." folder in folders: z.extractall(folder) #now save files specified location. print "download complete"
use z.read() or z.readstr() function extract file file , change paths and/or names need. odd extractall() overwriting existing files? if in each zip named same overwritten. simple that.
what doing here extracting same zip specified folders. loop has reworked follow zip's content, not other way around. assuming, of course, want achieve. can specify "c:\temp" extract whole zip (no loops needed) , deal resulting mess.
you said:
"the problem im using 4 different logins, each 1 has files , files have same name 1 in account."
but should deduced, comments copied above, have switch 2 loops. i.e. use new folder (from folders) per each username (from payloads. here extract same zip in folders. result being folders specified being filled zip last login. rework this.
a little example keeping design necessary changes:
from zipfile import zipfile cstringio import stringio import os temp_folder = "c:\\temp" users = { 123: ("login 123 etc", "download url 123"), 345: ("login 345 or url or whatever", "download url 345")} user in users: login_to(users[user][0]) content = get_zip_from(users[user][1]) path = os.path.join(temp_folder, user) if not os.path.exists(path): os.makedirs(path) z = zipfile(stringio(content)) z.extractall((path) reworking thing follow folders similar need connect folder user somehow. here used username folder extract content to. hope need.
Comments
Post a Comment