Пройдя много документации и выполнив поиск по стеку, я просто не смог найти решение своей проблемы.
По сути, я использую Beautifulsoup для очистки списка данных с веб-сайта, а затемсохранить его в Excel.Соскребание работает нормально.
Когда я запускаю свой скрипт, он выводит все элементы на терминал.Однако, когда я пытаюсь сохранить этот результат в dataframe и сохранить его в Excel, он выполнит только последнюю строку и сохранит ее в Excel.
Я пытался сохранить код внутри цикла, но результат тот же.Я попытался преобразовать список обратно в массив внутри цикла, но та же проблема.Тем не менее, последняя строка сохраняется только в Excel
Я думаю, что здесь не хватает логического подхода.Если бы кто-нибудь связал меня с тем, что искать, я был бы очень признателен.
soup = BeautifulSoup(html, features="lxml")
soup.find_all("div", {"id":"tbl-lock"})
for listing in soup.find_all('tr'):
listing.attrs = {}
assetTime = listing.find_all("td", {"class": "locked"})
assetCell = listing.find_all("td", {"class": "assetCell"})
assetValue = listing.find_all("td", {"class": "assetValue"})
for data in assetCell:
array = [data.get_text()]
### Excel Heading + data
df = pd.DataFrame({'Cell': array
})
print(array)
# In here it will print all of the data
### Now we need to save the data to excel
### Create a Pandas Excel writer using XlsxWriter as the Engine
writer = pd.ExcelWriter(filename+'.xlsx', engine='xlsxwriter')
### Convert the dataframe to an XlsxWriter Excel object and skip first row for custom header
df.to_excel(writer, sheet_name='SheetName', startrow=1, header=False)
### Get the xlsxwritert workbook and worksheet objects
workbook = writer.book
worksheet = writer.sheets['SheetName']
### Custom header for Excel
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#D7E4BC',
'border': 1
})
### Write the column headers with the defined add_format
print(df) ### In here it will print only 1 line
for col_num, value in enumerate(df):
worksheet.write(0, col_num +1, value, header_format)
### Close Pandas Excel writer and output the Excel file
writer.save()