Writing Python Selenium output to Excel

后端 未结 2 1026
逝去的感伤
逝去的感伤 2021-01-26 17:06

I have written a script to scrape product information from online websites. The goal is to write these information out to an Excel file. Due to my limited Python knowledge, I on

相关标签:
2条回答
  • 2021-01-26 17:39

    I usually find that writing to CSV is the safest way to get data into excel. I use something like the following code:

    import csv
    import sys
    import time
    import datetime
    from os import fsync
    
    ts=time.time() #get the time, to use in a filename
    ds=datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M') #format the time for the filename
    f2=open('OutputLog_'+ds+'.txt','w') #my file is output_log + the date time stamp
    f2.write(str('Column1DataPoint'+','+'Column2DataPoint') #write your text, separate your data with comma's
    #if you're running a long loop, and want to keep your file up to date with the proces do these two steps in your loop too
    f2.flush() 
    fsync(f2.fileno())
    
    #once the loop is finished and data is writtin, close your file
    f2.close()
    

    I think for you, the change to the above code would be to change the write line something like the following:

    f2.write(str(i+','+name.encode("utf-8")+','+prodNum+','+output.text)
    
    0 讨论(0)
  • 2021-01-26 17:58

    https://pythonhosted.org/openpyxl/tutorial.html

    This is a tutorial for a python library that allows manipulation for python There are other libraries but I like using this one.

    from openpyxl import Workbook wb = Workbook()

    then use the methods given to write your data and then

    wb.save(filename)

    really easy to get started.

    This is a pdf tutorial for using xlwt and xlrd, but I don't really use these modules alot. http://www.simplistix.co.uk/presentations/python-excel.pdf

    0 讨论(0)
提交回复
热议问题