Не получается парсить цену лекарств
Не получается парсить информацию о стоимости товара https://www.eapteka.ru/ Можете помочь пожалуйста.
import requests
from bs4 import BeautifulSoup
import time
url = "https://www.eapteka.ru/search/?sort=price&order=asc&q=%D0%90%D1%81%D0%BA%D0%BE%D1%80%D0%B1%D0%B8%D0%BD%D0%BA%D0%B0+%D0%B0%D1%81%D0%BA%D0%BE%D1%80%D0%B1%D0%B8%D0%BD%D0%BE%D0%B2%D0%B0%D1%8F+%D0%BA%D0%B8%D1%81%D0%BB%D0%BE%D1%82%D0%B0+%D0%BF%D0%BE%D1%80%D0%BE%D1%88%D0%BE%D0%BA+%D1%81%D0%BE+%D0%B2%D0%BA%D1%83%D1%81%D0%BE%D0%BC+%D0%BB%D0%B8%D0%BC%D0%BE%D0%BD%D0%B0"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Ищем все элементы с ценой
price_elements = soup.find_all('span', class_='listing-card__price-new')
if price_elements:
for price_element in price_elements:
price = price_element.get_text(strip=True)
print("Цена:", price)
else:
print("Цены не найдены.")
else:
print("Ошибка при получении страницы:", response.status_code)
time.sleep(5)
Ответы (1 шт):
Автор решения: tokimod
→ Ссылка
Вроде, решил твою проблему.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
import time
url = "https://www.eapteka.ru/search/?sort=price&order=asc&q=%D0%90%D1%81%D0%BA%D0%BE%D1%80%D0%B1%D0%B8%D0%BD%D0%BA%D0%B0+%D0%B0%D1%81%D0%BA%D0%BE%D1%80%D0%B1%D0%B8%D0%BD%D0%BE%D0%B2%D0%B0%D1%8F+%D0%BA%D0%B8%D1%81%D0%BB%D0%BE%D1%82%D0%B0+%D0%BF%D0%BE%D1%80%D0%BE%D1%88%D0%BE%D0%BA+%D1%81%D0%BE+%D0%B2%D0%BA%D1%83%D1%81%D0%BE%D0%BC+%D0%BB%D0%B8%D0%BC%D0%BE%D0%BD%D0%B0"
firefox_options = Options()
firefox_options.headless = True
driver = webdriver.Firefox(options=firefox_options)
driver.get(url)
time.sleep(10)
soup = BeautifulSoup(driver.page_source, 'html.parser')
# Ищем все элементы с ценой
price_elements = soup.find_all('span', class_='listing-card__price-new')
if price_elements:
for price_element in price_elements:
price = price_element.get_text(strip=True)
print("Цена:", price)
else:
print("Цены не найдены.")
driver.quit() # Закрываем браузер
Если у тебя firefox, то устанавливаешь pip install selenium geckodriver_autoinstaller, если chrome, то pip install selenium chromedriver_autoinstaller и меняешь строчку from selenium.webdriver.firefox.options import Options на from selenium.webdriver.chrome.options import Options