Writing the same thing in a CSV file using Python

I ran into a problem with my CSV record for a web clip project.

I got data formatted like this:

table = {
    "UR": url,
    "DC": desc,
    "PR": price,
    "PU": picture,
    "SN": seller_name,
    "SU": seller_url
}

What I get from a loop that parses an html page and returns this table to me. Basically, this table is fine, it changes every time I make a loop.

Now, when I want to write every table that I get from this loop into my CSV file, it will just write the same thing over and over again. The only element written is the first one I get with my loop and write it about 10 million times instead of about 45 times (articles on the page).

I tried to do this vanilla with the csv library and then with pandas.

So here is my loop:

if os.path.isfile(file_path) is False:
    open(file_path, 'a').close()
file = open(file_path, "a", encoding = "utf-8")

i = 1
while True:
    final_url = website + brand_formatted + "+handbags/?p=" + str(i)
    request = requests.get(final_url)
    soup = BeautifulSoup(request.content, "html.parser")
    articles = soup.find_all("div", {"class": "dui-card searchresultitem"})
    for article in articles:
        table = scrap_it(article)
        write_to_csv(table, file)
    if i == nb_page:
        break
    i += 1
file.close()

and here is my method for writing to the csv file:

def write_to_csv(table, file):
import csv

writer = csv.writer(file, delimiter = " ")
writer.writerow(table["UR"])
writer.writerow(table["DC"])
writer.writerow(table["PR"])
writer.writerow(table["PU"])
writer.writerow(table["SN"])
writer.writerow(table["SU"])

CSV Python , , . csv .

edit: img csv

enter image description here

, ,

EDIT: , , . , , , .

+4
1

,

import csv

fieldnames = ['UR', 'DC', 'PR', 'PU', 'SN', 'SU']    

def write_to_csv(table, file):
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writerow(table)

: https://docs.python.org/3/library/csv.html

+1

Source: https://habr.com/ru/post/1695462/


All Articles