Answer the question
In order to leave comments, you need to log in
Why is the console not displaying the parsing result?
When executing the code, the console does not display anything, although everything worked on the tutorial. I’m doing it on PyCharm, I thought that the problem arose due to a change on the site, but there were no changes (or I just didn’t notice)
C:\Users\Alikhan\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Alikhan/PycharmProjects/pythonProject/main.py
Process finished with exit code 0
import requests
from bs4 import BeautifulSoup
import csv
CSV = 'cards.csv'
HOST = 'https://kolesa.kz/'
URL = 'https://kolesa.kz/cars/audi/'
HEADERS = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/87.0.4280.88 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
}
def get_html(url, params=''):
r = requests.get(url, headers=HEADERS, params=params)
return r
def get_content(html, class_=None):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', target="product-item")
cards = []
print(items)
for item in items:
cards.append(
{
'title': item.find('div', class_='title').get.text(),
'link_product': HOST + item.find('div', class_='title').find('a').get('href'),
'brand': item.find('div', class_='brand').get.text(strip=True),
'card_image': HOST + item.find('div', class_='image').find('img').get('src'),
}
)
def save_doc(items, path):
with open(path, 'w', newline='') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(['Название продукта', 'Ссылка на продукт', 'банк', 'Изображение карты'])
for item in items:
writer.writerow([item['title'], item['link_product'], item['brand'], item['card_img']])
def parser():
PPAGENATION = input("Укажите страниц для парсинга: ")
PPAGENATION = int(PAGENATION.strip())
html = get_html(URL)
if html.status_code == 200:
cards = []
for page in range(1, PAGENATION):
print(f'Парсим: {page}')
html = get_html(URL, params={'page', page})
cards.extend(get_content(html.text))
save_doc(cards, CSV)
print(cards)
else:
print("Error.")
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question