-1

我有一个名为 MasterFile 的 Excel 文件。在 MasterFile 中,我有多个带有公式的工作表。我想使用下面的代码更新 MasterFile 中的一张表,而不会覆盖我的任何数据或公式。

到目前为止,这是我的代码:

from bs4 import BeautifulSoup
import requests
import pandas as pd
from openpyxl import load_workbook

url = 'http://www.baseballpress.com/lineups'

soup = BeautifulSoup(requests.get(url).text, 'html.parser')

players = [i.text for i in soup.find_all('a', {'class': 'player-link'})]


my_dict = (players)

df = pd.DataFrame(pd.Series(my_dict))

writer = pd.ExcelWriter('my2nd_webscrape.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()

我在如何在不破坏 openpyxl 的公式的情况下写入现有的 excel 文件中找到了一些关于这个主题的信息?,但我不确定如何调整我的代码。

4

1 回答 1

0

尝试这个:

from bs4 import BeautifulSoup
import requests
import pandas as pd
from openpyxl import load_workbook

book = load_workbook('my2nd_webscrape.xlsx')
writer = pd.ExcelWriter('my2nd_webscrape.xlsx')
writer.book = book

url = 'http://www.baseballpress.com/lineups'
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
players = [i.text for i in soup.find_all('a', {'class': 'player-link'})]
my_dict = (players)

df = pd.DataFrame(pd.Series(my_dict))
df.to_excel(writer,'Sheet1')
writer.save()
writer.close()
于 2018-09-08T09:27:01.707 回答