Both the Excel processing class and the upload class have been written, saved separately in excelhandle.py and upload.py, and finally we need to write a run.py to execute the upload script.
# !/usr/bin/python # -*-coding:utf-8-*- Import Excelhandle Import Upload Import Queue Import Time def run (filepath, username, password, url):
First introduce the Excelhandle,upload file, time packet, and queue package. The queue package is primarily used to create a single queues. Then the Run method passes 4 parameters, the path of the Excel file, the uploaded user name, the password, and the URL.
First instantiate a queue and a excelhandle, read the Excel data, and put the data in the queue:
Product_queue =Queue.queue ()#read Excel data join queueExcel_handle =Excelhandle.excelhandle (filepath) Excel_sheet=excel_handle.open_table (0) Product_data=excel_handle.foreach_data () Product_index=Excel_handle.get_index_name () forIinchRange (len (product_data)): Product_queue.put (product_data[i]) num=Str (i)Print "Data"+ num +"Standby"
Next, instantiate the upload class, log in to upload the site, and go to the upload page:
uploader = upload. Upprod () uploader.login (url,username,password) # Show upload button Uploader.display_list ()
Next use a while loop, when the queue is not empty, a queue of data from the queue to fill out, if the completion of a successful one, fill in the failure to put back into the queue to fill in the line. This takes advantage of the first-out features of this data structure of the queue. Then use the Create_log () method to create the log, when the queue is empty end loop, note that the data from Excel is not necessarily a string type, because the Selenium Send_keys () method does not accept data types such as floating point date, So there's a need for conversions:
uploader =upload. Upprod () uploader.login (Url,username,password)#Show Upload buttonuploader.display_list ()#extracting data from the queue and uploading it whileProduct_queue.empty () is notTrue:data=product_queue.get () proname=Data[product_index[0]] Scprice= STR (data[product_index[1]]) Sc_price= STR (data[product_index[2]]) Pfprice= STR (data[product_index[3]]) Sjprice= STR (data[product_index[4]]) Jsprice= STR (data[product_index[5]]) Point= STR (data[product_index[6]]) Ziti= STR (data[product_index[7]]) soldout= STR (data[product_index[8]) views= STR (data[product_index[9]]) know= data[product_index[10]] Detail= Data[product_index[11]] Date= data[product_index[12]] Order= data[product_index[13] ] Shop= data[product_index[14]] Imgpath="C:\cspy\webtest\img\\"+ Proname +". jpg" #determine if the upload was successful, and if not, re-enter the queue to upload ifuploader.up_product (Proname, Scprice, Sc_price, Pfprice, Sjprice, Jsprice, point, Ziti, soldout, views, know, detail, Date, order, shop, Imgpath): status= 1PrintProname +"\tupload Success" Else: Status=0 Uploader.create_log ("6.16up.txt", Proname,status)ifStatus = =0:product_queue.put (data) time.sleep (5)
Finally close the browser:
# close the browser when all uploads are complete Uploader.quit_window () print"uploadover"
OK, so that a run.py is written, and finally add a line of code
if __name__ ' __main__ ' : Run (R"C:\cspy\webtest\data\test.xlsx","admin ","password","http://www.xxx.com/admin" )
Run run.py in cmd to upload data effortlessly!
(Cspython) C:\cspy\webtest>python run.py
Automated upload Data (iii)