通过Win32扩展用列表填充Excel工作表的麻烦

时间:2019-02-21 03:53:45

标签: python excel win32com

我在最后一部分遇到麻烦,在那部分我用列表中的字典项填充字段。 运行代码时,出现第一行“ sh.Cells(row,1).Value = business ['address']”的错误,即:TypeError字符串索引必须是整数,而不是str。而且,Excel工作表没有填写。

        page = 1 
    fileobj = urllib.urlopen("http://canada411.yellowpages.ca/search/si/" + str(page) + "/Booster%20Juice/Canada") 

    s = fileobj.read() 
    resultlist = list(s)             

    pages = returnpages(s)

    for page in xrange(1,10):

        addresssplit = s.split('<div class="listing__address address mainLocal noNum">') # split the page up into a list based on this delimiter

        for item in addresssplit:
            address = parse(item,'<span class="jsMapBubbleAddress"  itemprop="streetAddress" >','</span>') # extract address
            city = parse(item,'<span class="jsMapBubbleAddress"  itemprop="addressLocality" >','</span>')
            province = parse(item,'<span class="jsMapBubbleAddress"  itemprop="addressRegion" >','</span>')
            postal = parse(item,'<span class="jsMapBubbleAddress"  itemprop="postalCode" >','</span>')
            phone = parse(item,'data-phone="','">')

            if len(address) > 0:
                resultlist.append({'address':address,'city':city, 'province':province,'postal':postal, 'phone':phone})



        fileobj = urllib.urlopen("https://www.yellowpages.ca/search/si/" + str(page + 1) + "/Booster%20Juice/Canada")
        s = fileobj.read()  

excelobj = win32com.client.Dispatch("Excel.Application")
    excelobj.Visible = 1                                                            
    excelobj.Workbooks.Add()                                                        
    sh = excelobj.ActiveSheet                                                       
    sh.Cells(1,1).Value = 'Address'                                                 
    sh.Cells(1,2).Value = 'City'                                                    
    sh.Cells(1,3).Value = 'Province'                                                
    sh.Cells(1,4).Value = 'Postal'                                                  
    sh.Cells(1,5).Value = 'Phone'                                                                                                      

    row = 1
for business in resultlist:                                                     
    sh.Cells(row,1).Value = business['address']                                 
    sh.Cells(row,2).Value = business['city']                                      
    sh.Cells(row,3).Value = business['province']                                
    sh.Cells(row,4).Value = business['postal']                                  
    sh.Cells(row,5).Value = business['phone']                                   
    row +=1      

0 个答案:

没有答案