有人知道这个写入excel里的具体操作吗,为什么我运行后只能重复打印第一行数据,希望能帮我解决一下,感激不尽。代码是能正常运行的,就是写入的只有重复的第一条代码..。

............................................................................................................................................................

img


def parse_data():
    #解析商品数据
    #多个商品数据解析
    workbook=xlsxwriter.Workbook('demo.xlsx')
    worksheet=workbook.add_worksheet()
    divs=driver.find_elements_by_xpath('//div[@class="grid g-clearfix"]/div/div')#加个s变成列表!!!
    for div in divs:
        info=div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text#//是跨阶定位,商品名称
        price=div.find_element_by_xpath('.//strong').text+'元'#价格
        deal=div.find_element_by_xpath('.//div[@class="deal-cnt"]').text#付款人数

        name=div.find_element_by_xpath('.//div[@class="shop"]/a/span[2]').text#店名
        location=div.find_element_by_xpath('.//div[@class="location"]').text#地址
#        deal_url=div.find_element_by_xpath('.//div[@class="pic"]/a').get_attribute('href')
        worksheet.write('A',info)
        worksheet.write('B',price)
        worksheet.write('C',deal)
        worksheet.write('D',name)
        worksheet.write('F',location)
        workbook.close()

你好,还是先汇总再写入吧!

 
def parse_data():
    #解析商品数据
    #多个商品数据解析
    workbook=xlsxwriter.Workbook('demo.xlsx')
    worksheet=workbook.add_worksheet()
    divs=driver.find_elements_by_xpath('//div[@class="grid g-clearfix"]/div/div')#加个s变成列表!!!
    info = []
    price = []
    deal = []
    name = []
    location = []
    for div in divs:
        info.append(div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text)#//是跨阶定位,商品名称
        price.append(div.find_element_by_xpath('.//strong').text+'元')#价格
        deal.append(div.find_element_by_xpath('.//div[@class="deal-cnt"]').text)#付款人数
        name.append(div.find_element_by_xpath('.//div[@class="shop"]/a/span[2]').text)#店名
        location.append(div.find_element_by_xpath('.//div[@class="location"]').text)#地址
#        deal_url=div.find_element_by_xpath('.//div[@class="pic"]/a').get_attribute('href')
    worksheet.write('A',info)
    worksheet.write('B',price)
    worksheet.write('C',deal)
    worksheet.write('D',name)
    worksheet.write('F',location)
    workbook.close()

因为你要一个单元格一个单元格的写。
你这样写只选中了第一个单元格。