ошибка в работе парсера
Ошибки в консоли нет, однако в консоли нет и конечного кода.
Все что выводиться в консоли: Process finished with exit code 0
Сам код:
import requests
from bs4 import BeautifulSoup
def parse():
URL = 'https://www.olx.ua/elektronika/kompyutery-i-komplektuyuschie/'
HEADERS = {
'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 OPR/72.0.3815.465'
}
response = requests.get(URL, HEADERS)
soup = BeautifulSoup(response.content, 'html.parser')
items = soup.find_all('div', class_='offer-wrapper')
comps = []
for item in items:
comps.append({
'title': item.find('a', class_='marginright5 link linkWithHash detailsLink linkWithHashPromoted').get_text,
'price': item.find('p', class_='price').get_text(strip=True)
})
for comp in comps:
print(f"{comp['title']}")
parse()
Ответы (1 шт):
Автор решения: Іван Шнір
→ Ссылка
Чуть поправил ваш код, ошибка была в том что команды get_text() нету только есть .text и бс4 не выдел класс ссылки(это бывает когда класс слишком длинный).Вот сам код который работает:
from bs4 import BeautifulSoup
def parse():
URL = 'https://www.olx.ua/elektronika/kompyutery-i-komplektuyuschie/'
HEADERS = {
'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 OPR/72.0.3815.465'
}
response = requests.get(URL, HEADERS)
soup = BeautifulSoup(response.content, 'html.parser')
items = soup.find_all('div', class_='offer-wrapper')
comps = []
for item in items:
comps.append({
'title': item.find('h3', class_='lheight22 margintop5').text.strip(),
'price': item.find('p', class_='price').text.strip()
})
for comp in comps:
print(f"{comp['title']} : {comp['price']}")
parse()