import requests
from bs4 import BeautifulSoup
import pandas as pd
def fetch_html(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.content
except requests.RequestException as e:
print(f"Request error: {e}")
return None
def parse_complex_details(item):
details = {
'Фотографии': [],
'Застройщик': '',
'Площадь': '',
'Стоимость': '',
'Тип отделки': '',
'Количество корпусов': '',
'Видеонаблюдение': '',
'Этап строительства': '',
'Этажность': '',
'Срок сдачи': '',
'Меблировка': '',
'Балкон': ''
}
# Извлечение фотографий
photos = item.select('.project-list-item__img-wrapper img')
details['Фотографии'] = [photo['src'] for photo in photos] if photos else []
# Извлечение информации о застройщике, площади и стоимости
developer_info = item.select_one('.project-list-item__content_main .project-list-item__content_list > div:nth-child(2)')
details['Застройщик'] = developer_info.get_text(strip=True) if developer_info else 'Информация о застройщике отсутствует'
area_info = item.select_one('.project-list-item__content_main .project-list-item__content_list > div:nth-child(3)')
details['Площадь'] = area_info.get_text(strip=True) if area_info else 'Информация о площади отсутствует'
price_info = item.select_one('.project-list-item__content_bottom > div > span.d-block.text-h10')
details['Стоимость'] = price_info.get_text(strip=True) if price_info else 'Информация о стоимости отсутствует'
# Извлечение типа отделки из предоставленного селектора
finish_type_info = item.select_one('.project-finish-type')
details['Тип отделки'] = finish_type_info.get_text(strip=True) if finish_type_info else 'Информация о типе отделки отсутствует'
# Обработка нового селектора для извлечения дополнительных деталей
additional_details_content = item.select_one('#__nuxt > div > div.default-layout > div > main > div.project-detailed > div:nth-child(2) > div > div.project-characteristics-grid > div:nth-child(7) > div.project-characteristics-grid__item_content')
if additional_details_content:
characteristics = additional_details_content.select('.project-characteristics-grid__item_label')
values = additional_details_content.select('.project-characteristics-grid__item_value')
for characteristic, value in zip(characteristics, values):
label = characteristic.get_text(strip=True)
value_text = value.get_text(strip=True)
if "Тип отделки" in label:
details['Тип отделки'] = value_text
elif "Количество корпусов" in label:
details['Количество корпусов'] = value_text
elif "Видеонаблюдение" in label:
details['Видеонаблюдение'] = value_text
elif "Этап строительства" in label:
details['Этап строительства'] = value_text
elif "Этажность" in label:
details['Этажность'] = value_text
elif "Срок сдачи" in label:
details['Срок сдачи'] = value_text
elif "Меблировка" in label:
details['Меблировка'] = value_text
elif "Балкон" in label:
details['Балкон'] = value_text
return details
def parse_complexes_list(url):
html_content = fetch_html(url)
if not html_content:
return []
soup = BeautifulSoup(html_content, 'html.parser')
complexes = []
complex_items = soup.select('.project-list a')
for item in complex_items:
name = item.select_one('.project-list-item__content > div')
name_text = name.get_text(strip=True) if name else 'Название не найдено'
details = parse_complex_details(item)
complexes.append({
'Название': name_text,
**details,
})
return complexes
def main():
urls = [
"https://th.housebook.deals/ru/zhilye-kompleksy",
"https://ae.housebook.deals/ru/zhilye-kompleksy",
"https://th.housebook.deals/ru/zhilye-kompleksy/hennessy-residence-98ce6b"
]
all_complexes = []
for url in urls:
complexes = parse_complexes_list(url)
all_complexes += complexes
print(f"Found {len(complexes)} complexes at {url}")
df = pd.DataFrame(all_complexes)
excel_path = 'complexes_data.xlsx'
df.to_excel(excel_path, index=False)
print(f"Data saved to {excel_path}")
if __name__ == "__main__":
main()