简体   繁体   English

无法使用openpyxl将数据附加到Excel文件中的工作表中

[英]Unable to append data to a worksheet in an excel file using openpyxl

How can I append data to my existing worksheet using openpyxl? 如何使用openpyxl将数据附加到现有工作表中? I've created a scraper in python which is able to grab titles and links from a webpage. 我在python中创建了一个刮板,可以从网页中获取标题和链接。 I wish to write them in an excel file using openpyxl. 我希望使用openpyxl将它们写在excel文件中。 However, my scraper neither writes those newly populated data in the excel file nor throws an error? 但是,我的抓取工具既不将那些新填充的数据写入excel文件,也不引发错误? Where am I going wrong? 我要去哪里错了? Thanks in advance to take a look into it. 预先感谢您来看看它。

Here is my try: 这是我的尝试:

import requests
from bs4 import BeautifulSoup
from openpyxl import load_workbook

wb = load_workbook("scraper.xlsx")
ws = wb.worksheets[0]

res = requests.get("https://www.yify-torrent.org/search/1080p/")
soup = BeautifulSoup(res.text, 'lxml')
for item in soup.select(".mv"):
    name = item.select("a")[0].text
    link = item.select("a")[0]['href']
    ws.append([name,link])

The modifications are still in the memory. 修改仍在内存中。 Add wb.save("new file.xlsx") at the end of the code. 在代码末尾添加wb.save("new file.xlsx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM