I extract html data from a mail and parse this data with beautifulsoup. Next, I want to store the parsed data under the right headers in the csv file. However, the text of the input data does not show accordingly in the output csv file.
Parsed input data (fruits_html) for csv file:
Apples 43 0 0 0<br/>
Bananas 2282 0 500 0<br/>
Grapes 2534 0 500 0<br/>
Oranges 274 0 0 0<br/>
--------------------------------------------------------------------------------------------------<br/>
Script:
# Parse raw messages to something readable
soup = BeautifulSoup(raw_email, 'html.parser')
fruits_html = soup.find_all('span')
headers = ["Names", "Quantity", "SpareQty", "MinQty", "MaxQty"]
with open('output.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output, delimiter=',')
csv_output.writerow(headers)
for br in soup.find_all('span'):
csv_output.writerow([fruits_html for br in br.find_all('br')])
Desired output:
I want to store all the quantities under the right header in the csv file. Unfortunately, my current output shows the headers in the first row, and in the second row a large number of <br/> in different cells.
fruits.htmlcould help.fruits_htmlwithcsv_output.writerow([fruits_html for br in br.find_all('br')])right?