Склейка одного слова с каждым элементом списка с помощью .join()
Нужно склеить с помощью .join() цену и валюту(например 7000 и руб.)
7000 - hotel_price
руб. - hotel_price2
из за того что цен много то он склеивает только последнюю цену, а мне нужно все
import requests
import lxml
from bs4 import BeautifulSoup
import csv
def get_data(url):
headers = {
'User Agent': 'Mozilla / 5.0(WindowsNT10.0; Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 110.0.0.0 Safari / 537.36'
}
req = requests.get(url=url)
#with open('index.html', 'w', encoding='utf8') as file:
#file.write(req.text)
soup = BeautifulSoup(req.text, 'lxml')
hotel_cards = soup.find_all('li', class_='item')
#print(hotel_cards)
for hotel_url in hotel_cards:
hotel_url = 'https://101hotels.com' + hotel_url.find('a').get('href')
#print(hotel_url)
for title_hotels in hotel_cards:
title_hotels = title_hotels.find('a').text
#print(title_hotels)
for hotel_price in hotel_cards:
hotel_price = hotel_price.find('span', class_='price-highlight').text.strip()
print(hotel_price)
for hotel_price2 in hotel_cards:
hotel_price2 = hotel_price2.find('span', class_='currency').text.strip()
print(hotel_price2)
hotel_price_result_str = " ".join()
def main():
get_data('https://101hotels.com/main/cities/yuzhno-sakhalinsk')
if __name__ == '__main__':
main()
Ответы (2 шт):
Автор решения: arnold
→ Ссылка
Так должно всё сработать:
import requests
import lxml
from bs4 import BeautifulSoup
import csv
def get_data(url):
headers = {
'User Agent': 'Mozilla / 5.0(WindowsNT10.0; Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 110.0.0.0 Safari / 537.36'
}
req = requests.get(url=url)
soup = BeautifulSoup(req.text, 'lxml')
hotel_cards = soup.find_all('li', class_='item')
for hotel_url in hotel_cards:
hotel_url = 'https://101hotels.com' + hotel_url.find('a').get('href')
for title_hotels in hotel_cards:
title_hotels = title_hotels.find('a').text
for hotel in hotel_cards:
hotel_price = hotel.find('span', class_='price-highlight').text.strip()
hotel_price2 = hotel.find('span', class_='currency').text.strip()
price_with_currency = f'{hotel_price} - {hotel_price2}'
print(price_with_currency)
def main():
get_data('https://101hotels.com/main/cities/yuzhno-sakhalinsk')
if __name__ == '__main__':
main()
Автор решения: Namerek
→ Ссылка
Собираем данные:
from bs4 import BeautifulSoup as Soup, Tag
from requests import Session
with Session() as s:
response = s.get(
'https://101hotels.com/main/cities/yuzhno-sakhalinsk'
)
soup = Soup(response.content, 'html.parser')
items = soup.select('li.item')
entries = []
empty_price = Tag(name='price', attrs={})
empty_rating = Tag(name='rating', attrs={})
empty_name = Tag(name='name', attrs={})
for entry in items:
entries.append(
dict(
title=(entry.select_one('.item-content [itemprop="name"]') or empty_name).get_text(strip=True),
price=int((entry.select_one('.price-value') or empty_price).get('data-price-value') or 0),
rating=(entry.select_one('.rating-text-description') or empty_rating).get_text(strip=True) or None,
services=', '.join(item.get('content') for item in entry.select('[itemprop="amenityFeature"] [itemprop="name"]')),
ad=', '.join(item.get('title') for item in entry.select('.hotel_header-labels__item'))
)
)
Сохраняем в excel:
import pandas as pd
pd.DataFrame(entries).to_excel('hotels.xlsx', engine='openpyxl', index=False)
Сохраняем в csv:
import csv
with open('hotels.csv', 'w', encoding='utf-8') as file:
wrt = csv.DictWriter(
file,
fieldnames=entries[0].keys(),
dialect=csv.unix_dialect
)
wrt.writeheader()
wrt.writerows(
entries
)