I didn't look at your code into depth. Now I see see it's a bit weird. You iterate over bunch of files, to read first title in separate list, then again to read address(es).
There is no need to use find_all for
Then, is it one address or multiple in each file?
You write to a file after you have exited the second loop. But using list-comprehension will give you only the data from last file not all files (i.e. like when you append to a single list) - this is something I overlooked.
Finally if write 2 lists, but I don't think it will give you what you expect anyway.
There is no need to use find_all for
title
- it is expected to have only one tag title, right? Just use soup.find()
Then, is it one address or multiple in each file?
You write to a file after you have exited the second loop. But using list-comprehension will give you only the data from last file not all files (i.e. like when you append to a single list) - this is something I overlooked.
Finally if write 2 lists, but I don't think it will give you what you expect anyway.
import csv path = "C:\\Users\\mzoljan\\Downloads\\lksd\\" for infile in glob.glob(os.path.join(path, "*.html")): with open(infile, "r") as f, open('output2.csv', 'а') as myfile: writer = csv.writer(myfile) soup = BeautifulSoup(f.read(), 'lxml') title = soup.find("title") if title: title = soup.title.string else: title = '' # just in case there is no title tag address = soup.find_all("address", class_={"styles_address__zrPvy"}) # do you really need find_all? for item in address: writer.writerow([title, item.string])Note, the code is not tested as I don't have your html files.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs