Я так понимаю, надо с записью в CSV-файл?
import csv
import unicodedata
import requests
from anti_useragent import UserAgent
from bs4 import BeautifulSoup
ua = UserAgent()
headers = {'User-Agent': ua.chrome}
def get_html(url):
response = requests.get(url, headers=headers)
return response.text
def get_content(html):
soup = BeautifulSoup(html, 'lxml')
items = soup.find_all('a', class_='css-yo21t ewrty961')
cars = []
for item in items:
cars.append({
'Title': item.find('div', class_='css-17lk78h e3f4v4l2').text.strip(),
'Price': unicodedata.normalize("NFKD", item.find('div', class_='css-1dv8s3l eyvqki91').text.strip()),
'About': item.find('div', class_='css-1fe6w6s e162wx9x0').text.strip()
})
return cars
def content_to_csv(content):
columns = ['Title', 'Price', 'About']
result_file = 'autodrom.csv'
try:
with open(result_file, 'w', encoding='utf-8', newline='') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=columns, dialect='unix')
writer.writeheader()
for item in content:
writer.writerow(item)
except IOError as err:
print(f'Error writing the file {result_file}: {err}')
def main():
url = 'https://auto.drom.ru/audi/'
content_to_csv(get_content(get_html(url)))
if __name__ == '__main__':
main()