Я пытаюсь захватить div с классом, но не могу по какой-то причине. Идентификатор есть, но для каждого продукта он разный. Как мне успешно получить
Это мой код;
from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup my_url = "https://meqasa.com/apartments-for-sale-in-Accra" uClient = uReq(my_url) page_html = uClient.read() uClient.close() page_soup = soup(page_html, "html.parser") containers = page_soup.findAll("div", {"class": "row mqs-prop-inner-wrap with featt"}) print(len(containers))
Вы можете использовать CSS селектор div[id^="feli"]. При этом будут выбраны все теги <div>, id= которых начинаются с "feli".
div[id^="feli"]
<div>
id=
Например:
import requests from bs4 import BeautifulSoup url = 'https://meqasa.com/apartments-for-sale-in-Accra' soup = BeautifulSoup(requests.get(url).content, 'html.parser') for item in soup.select('div[id^="feli"]'): print(item.h2.get_text(strip=True)) print('https://meqasa.com' + item.a['href']) print('-' * 80)
Печать:
3 bedroom apartment for sale at Community 25, Tema, Tema, Greater Accra Region https://meqasa.com/3-bedroom-apartment-for-sale-in-Community 25, Tema, Tema, Greater Accra Region, Ghana-unit-1411 -------------------------------------------------------------------------------- 2 bedroom apartment for sale at Sakumono https://meqasa.com/2-bedroom-apartment-for-sale-in-Sakumono-unit-1385 -------------------------------------------------------------------------------- 1 bedroom apartment for sale at East Legon https://meqasa.com/1-bedroom-apartment-for-sale-in-East Legon-unit-1383 -------------------------------------------------------------------------------- 2 bedroom apartment for sale at Accra https://meqasa.com/2-bedroom-apartment-for-sale-in-Accra-unit-1408 -------------------------------------------------------------------------------- 1 bedroom apartment for sale at Sakumono https://meqasa.com/1-bedroom-apartment-for-sale-in-Sakumono-unit-1363 -------------------------------------------------------------------------------- ... and so on.