【发布时间】:2020-11-13 18:21:49
【问题描述】:
我正在构建一个小工具来检查网站上的超链接是否会消失或保留。 该程序到目前为止工作正常,但总是创建一个新文件并删除一天前创建的最新工作表(即每天的脚本)。
我的代码如下
def save_results(excel_urls,erg):
currentdate = str(date.today())
excel_file = openpyxl.load_workbook('Links2.xlsx')
excel_sheet = excel_file['Sheet']
book = Workbook()
sheet = book.active
#this following code is from my main sheet to keep it in the following file as well. its also not a proper update function but works so far.
sheet.column_dimensions['A'].width = 60
sheet.column_dimensions['B'].width = 60
for row in excel_sheet.values:
sheet.append(row)
#Here im going to create the other sheets with the results of my crawl
book.create_sheet(currentdate)
book[currentdate].column_dimensions['A'].width = 60
book[currentdate].column_dimensions['B'].width = 60
for keys,values in enumerate(excel_urls):
y = values[0], str(erg[keys])
book[currentdate].append(y)
book.save('Links2.xlsx')
我找不到只更新而不覆盖现有文件的方法。在保护结果之前包含读取的所有内容的功能也可以正常工作。
提前感谢您的帮助!
我的功能并没有真正起作用。
excel_file = openpyxl.load_workbook('Links2.xlsx')
book = Workbook()
for z,i in enumerate(excel_file.sheetnames):
book.create_sheet('{}'.format(i))
print(excel_file.sheetnames[z])
for row in excel_file[excel_file.sheetnames[i]].values:
#print(row)
excel_file[excel_file.sheetnames[i]].append(row)
book.save('Links2.xlsx')```
【问题讨论】:
-
更新和覆盖没有区别。
-
谢谢,我早就料到了。所以我尝试编写一个将所有现有数据附加到文件的函数,但它不起作用。将在我的帖子中添加代码。
标签: python-3.x beautifulsoup web-crawler openpyxl