Не записывает в csv-файл
Это основной код откуда я вызываю insert
.
import requests
from bs4 import BeautifulSoup
import pandas
import data_client
class Parser:
links_to_parse = [
'https://www.kufar.by/l/mebel',
'https://www.kufar.by/l/mebel?cursor=eyJ0IjoiYWJzIiwiZiI6dHJ1ZSwicCI6MiwicGl0IjoiMjg1NTQzNzIifQ%3D%3D',
'https://www.kufar.by/l/mebel?cursor=eyJ0IjoiYWJzIiwiZiI6dHJ1ZSwicCI6MywicGl0IjoiMjg1NTQzNzgifQ%3D%3D',
'https://www.kufar.by/l/mebel?cursor=eyJ0IjoiYWJzIiwiZiI6dHJ1ZSwicCI6NSwicGl0IjoiMjg1NTQ1MDkifQ%3D%3D'
]
data_client_imp = data_client.Sqlite3Client()
@staticmethod
def get_mebel_by_link(link):
response = requests.get(link)
mebel_data = response.text
mebel_items = []
to_parse = BeautifulSoup(mebel_data, 'html.parser')
for elem in to_parse.find_all('a', class_='styles_wrapper__5FoK7'):
try:
price, description = elem.text.split('р.')
mebel_items.append((
elem['href'],
int(price.replace(' ', '')),
description
))
except:
print(f'Цена не была указана. {elem.text}')
return mebel_items
@staticmethod
def save_to_csv(mebel_items):
pandas.DataFrame(mebel_items).to_csv('mebel.csv', index=False)
def save_to_postgres(self, mebel_items):
connection = self.data_client_imp.get_connection()
self.data_client_imp.create_mebel_table(connection)
for item in mebel_items:
self.data_client_imp.insert(connection, item[0], item[1], item[2])
def run(self):
mebel_items = []
for link in Parser.links_to_parse:
mebel_items.extend(self.get_mebel_by_link(link))
self.save_to_postgres(mebel_items)
self.save_to_csv(mebel_items)
Parser().run()
Код модуля data_client
:
import sqlite3
from sqlite3 import Error
import psycopg2
import csv
from abc import ABC, abstractmethod
class DataClient(ABC):
@abstractmethod
def get_connection(self):
pass
@abstractmethod
def create_mebel_table(self, conn):
pass
@abstractmethod
def get_items(self, conn, price_from=0, price_to=100000):
pass
@abstractmethod
def insert(self, conn, link, price, description):
pass
def run_test(self):
conn = self.get_connection()
self.create_mebel_table(conn)
items = self.get_items(conn, price_from=10, price_to=30)
for item in items:
print(item)
class CsvClient(DataClient):
FILENAME = 'mebel.csv'
def get_connection(self):
return None
def create_mebel_table(self, conn):
pass
def get_items(self, conn, price_from=0, price_to=100000):
items = []
with open(self.FILENAME, 'r', encoding='utf-8') as csvfile:
file_reader = csv.reader(csvfile, delimiter=",")
count = 0
for row in file_reader:
price = int(row[1])
if count == 0:
print(f'Файл содержит столбцы: {",".join(row)}')
elif not price <= price_from and price <= price_to:
items.append(row)
count += 1
return items
def insert(self, conn, link, price, description):
with open(self.FILENAME, 'a', encoding='utf-8') as csvfile:
file_writer = csv.writer(csvfile, delimiter=":", lineterminator="\r")
file_writer.writerow(['link', 'price', 'description'])
file_writer.writerow([f"'{link}', '{price}', '{description}'"])
data_client = CsvClient()
data_client.run_test()
Ответы (1 шт):
Автор решения: Валерий
→ Ссылка
with open('name_file.ext', mode="w", encoding='windows-1251') as w_file:
file_writer = csv.writer(w_file, delimiter="|", lineterminator="\r")
file_writer.writerow([data_for_write1])
Думаю mode='a' надо заменить на 'w'