(Jul-08-2019, 12:16 PM)starter_student Wrote: and now there is no error but the output file is empty just with headersThat's because your parsing or something else is wrong.
Do test is small step,put in
print()
and do test in REPL.store_details = {}
should be outside of the loop.The html code you posted it's just a mess.
To show how can test html code outside of a web-site.
from bs4 import BeautifulSoup as soup import csv import requests html = '''\ <div id="storelist"> <ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul> </div>''' #code from bs4 import BeautifulSoup as soup import csv import requests #URL = "http:www.abc.com" #r = requests.get(URL) soup = BeautifulSoup(html, 'lxml') table = soup.find('div', id="storelist") print(table) # Test print store_details = {} for row in table.find_all('li'): store_details[row.text] = f'<{row.text}> parsed for site' filename = 'store_details_tab.csv' with open(filename, 'w') as f: w = csv.DictWriter(f, ['Coffee', 'Tea', 'Milk']) w.writeheader() w.writerow(store_details)In csv:
Output:Coffee,Tea,Milk
<Coffee> parsed for site,<Tea> parsed for site,<Milk> parsed for site