Код: Выделить всё
import os
import pandas as pd
import re
directory_path = r"C:\Users\..."
descriptions = []
prices = []
review_numbers = []
file_names = []
stars = []
for file_name in os.listdir(directory_path):
if file_name.endswith(".txt"):
file_path = os.path.join(directory_path, file_name)
with open(file_path, "r", encoding="utf-8") as file:
lines = file.readlines()
description = ""
price = ""
review_number = ""
star_rating = ""
for line in lines:
if line.startswith('----------------------------------------'):
if description != "":
descriptions.append(description.strip())
prices.append(price)
review_numbers.append(review_number)
file_names.append(file_name)
stars.append(star_rating)
description = ""
price = ""
review_number = ""
star_rating = ""
elif re.match(r'\$\d+(?:\.\d+)?', line):
price = line.strip()
elif re.search(r'\(\d[\d,]*\)', line):
review_number = re.search(r'\(\d[\d,]*\)', line).group()
else:
description += line.strip() + " "
star_match = re.search(r'class="wt-align-items-center wt-max-height-full wt-display-flex-xs flex-direction-row-xs wt-text-title-small wt-no-wrap">([\d.]+)', line)
if star_match:
star_rating = star_match.group(1)
if 'v2-listing-card_title' in line:
description = line.split('title="', 1)[1].split('"', 1)[0]
if description != "":
descriptions.append(description.strip())
prices.append(price)
review_numbers.append(review_number)
file_names.append(file_name)
stars.append(star_rating)
data = {'Description': descriptions, 'Price': prices, 'Review_Number': review_numbers, 'File_Name': file_names, 'Stars': stars}
df = pd.DataFrame(data)
https://www.etsy.com/search ?q=halloween+shirt&explicit=1&ship_to=FR&price_bucket=1&max=20&ref=search_bar
Фрагмент HTML, содержащий звезды, выглядит следующим образом:
Код: Выделить всё
flex
Flex $0
flex
"4.9"[/b]
Описание
Цена
Review_Number
Имя_файла
Звезды
< tbody>
Наклеенные термонаклейки на обувь на свободе ...
$6,89
(801 )
halloween_cape_0-20_clean.txt
Милый кот в шляпе ведьмы Наклейка / Милый котенок...
$3,68
(30)
halloween_cape_0-20_clean.txt
Темно-синий бархатный костюм волшебника Юбка-пачка-накидка Ван...
$18,57
(3,990)
halloween_cape_0-20_clean.txt< /td>
Накидка для собаки - «КОРОЛЕВА ЕЛИЗАВЕТА» ...
$17,26
(25)
halloween_cape_0-20_clean.txt
< /td>
Картина кота-вампира на Хэллоуин, кот на Хэллоуин...
$25,69 td>
(427)
halloween_cape_0-20_clean.txt
...
...
...
...
...
Ожерелье Джек Скеллингтон с галстуком-бабочкой размера XXL - Глаза Т...
$57,52
(104)
halloween_tie_50 -100_clean.txt
Винтажный роман «Хэллоуинский сезон ведьмы». ..
$79,57
(1,650)
halloween_tie_50-100_clean.txt
Толстовка «Хэллоуин тай-дай» // Оранжевый, Фиолетовый...
50,00 долларов США
(755)
halloween_tie_50-100_clean.txt
Загрузка...
halloween_tie_50-100_clean.txt
< /таблица>
Подробнее здесь: https://stackoverflow.com/questions/777 ... ith-python