Last active
October 19, 2021 18:49
-
-
Save lananovikova10/743956c3a3eff6e452a8fa52315e8f94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, string | |
from bs4 import BeautifulSoup | |
import log | |
def get_info_rosturizm(country_arr): | |
url = "https://city.russia.travel/safety/kakie_strany_otkryty/" | |
html = get_html(url) | |
if html: | |
data = parse_conditions_rosturizm(html, country_arr) | |
return data | |
else: | |
return None | |
def get_html(url): | |
try: | |
result = requests.get(url) | |
result.raise_for_status() | |
return result.text | |
except(requests.RequestException, ValueError): | |
return None | |
def parse_conditions_rosturizm(html, country_arr): | |
soup = BeautifulSoup(html, 'html.parser') | |
all_published_country = soup.findAll('div', class_='t336__title t-title t-title_md', field="title") | |
for item in all_published_country: | |
if item.text == country_arr: | |
info_block = item.find_next('div', class_='t-text t-text_md') | |
return get_conditions(info_block) | |
return {} | |
def get_conditions(info_block): | |
country_conditions = {} | |
conditions_info = info_block.findAll('strong') | |
log.logging.info(type(conditions_info)) | |
# truth = 'Прямое авиасообщение' in conditions_info | |
# for i in conditions_info: | |
# if "Транспортное сообщение" or 'Прямое авиасообщение' or "Прямое чартерное авиасообщение" or 'Авиасообщение с пересадками' and "Виза" in i.text: | |
# # country_conditions['transportation'] = info_block.text.split('Транспортное сообщение')[1].split('Виза')[0].strip(string.punctuation).strip() | |
# # return country_conditions | |
# print(truth) | |
# [<strong>Прямое авиасообщение</strong>, <strong>Виза: </strong>, <strong>Условия въезда</strong>, <strong>Дополнительно:</strong>, <strong>Какие вакцины признаются: </strong>, <strong>Что открыто: </strong>, <strong>Ограничения:</strong>, <strong>Полезные телефоны</strong>] | |
for i in conditions_info: | |
if i.text == 'Транспортное сообщение': | |
country_conditions['transportation'] = info_block.text.split('Транспортное сообщение')[1].split('Виза')[0].strip(string.punctuation).strip() | |
elif i.text == 'Прямое авиасообщение': | |
country_conditions['transportation'] = i.text.strip(string.punctuation).strip() | |
elif i.text == 'Прямое чартерное авиасообщение': | |
country_conditions['transportation'] = i.text.strip(string.punctuation).strip() | |
elif i.text == 'Авиасообщение с пересадками': | |
country_conditions['transportation'] = i.text.strip(string.punctuation).strip() | |
if i.text == 'Дополнительно:': | |
country_conditions['visa'] = info_block.text.split('Виза')[1].split('Дополнительно')[0].strip(string.punctuation).strip() | |
country_conditions['conditions'] = info_block.text.split('Дополнительно')[1].split('Какие вакцины признаются')[0].strip(string.punctuation).strip() | |
log.logging.info('dopolnitelno est') | |
else: | |
country_conditions['visa'] = info_block.text.split('Виза')[1].split('Условия въезда')[0].strip(string.punctuation).strip() | |
country_conditions['conditions'] = info_block.text.split('Условия въезда')[1].split('Какие вакцины признаются')[0].strip(string.punctuation).strip() | |
log.logging.info('NET!!!') | |
country_conditions['vaccine'] = info_block.text.split('Какие вакцины признаются')[1].split('Что открыто')[0].strip(string.punctuation).strip() | |
if i.text == 'Ограничения:': | |
country_conditions['open_objects'] = info_block.text.split('Что открыто')[1].split('Ограничения')[0].strip(string.punctuation).strip() | |
country_conditions['restrictions'] = info_block.text.split('Ограничения')[1].split('Полезные телефоны')[0].strip(string.punctuation).strip() | |
log.logging.info('ogranichenya!!!') | |
elif i.text == 'Полезные телефоны': | |
pass | |
else: | |
log.logging.info('Данные об ограничениях не пришли') | |
country_conditions['open_objects'] = info_block.text.split('Что открыто')[1].split('Полезные телефоны')[0].strip(string.punctuation).strip() | |
country_conditions['restrictions'] = 'Нет данных' | |
return country_conditions | |
country_arr = 'Азербайджан' | |
print(get_info_rosturizm(country_arr)) | |
import requests, string | |
from bs4 import BeautifulSoup | |
import log | |
def get_info_rosturizm(country_arr): | |
url = "https://city.russia.travel/safety/kakie_strany_otkryty/" | |
html = get_html(url) | |
if html: | |
data = parse_conditions_rosturizm(html, country_arr) | |
return data | |
else: | |
return None | |
def get_html(url): | |
try: | |
result = requests.get(url) | |
result.raise_for_status() | |
return result.text | |
except(requests.RequestException, ValueError): | |
return None | |
def parse_conditions_rosturizm(html, country_arr): | |
soup = BeautifulSoup(html, 'html.parser') | |
all_published_country = soup.findAll('div', class_='t336__title t-title t-title_md', field="title") | |
for item in all_published_country: | |
if item.text == country_arr: | |
info_block = item.find_next('div', class_='t-text t-text_md') | |
return get_conditions(info_block) | |
return {} | |
def get_conditions(info_block): | |
country_conditions = {} | |
conditions_info = info_block.findAll('strong') | |
log.logging.info(type(conditions_info)) | |
for i in conditions_info: | |
if i.text == 'Транспортное сообщение': | |
country_conditions['transportation'] = info_block.text.split('Транспортное сообщение')[1].split('Виза')[0].strip(string.punctuation).strip() | |
elif i.text == 'Прямое авиасообщение': | |
country_conditions['transportation'] = i.text.strip(string.punctuation).strip() | |
elif i.text == 'Прямое чартерное авиасообщение': | |
country_conditions['transportation'] = i.text.strip(string.punctuation).strip() | |
elif i.text == 'Авиасообщение с пересадками': | |
country_conditions['transportation'] = i.text.strip(string.punctuation).strip() | |
else: | |
if i.text == 'Ограничения:': | |
country_conditions['open_objects'] = info_block.text.split('Что открыто')[1].split('Ограничения')[0].strip(string.punctuation).strip() | |
country_conditions['restrictions'] = info_block.text.split('Ограничения')[1].split('Полезные телефоны')[0].strip(string.punctuation).strip() | |
log.logging.info('ogranichenya!!!') | |
elif i.text == 'Полезные телефоны': | |
pass | |
else: | |
log.logging.info('Данные об ограничениях не пришли') | |
country_conditions['open_objects'] = info_block.text.split('Что открыто')[1].split('Полезные телефоны')[0].strip(string.punctuation).strip() | |
country_conditions['restrictions'] = 'Нет данных' | |
country_conditions['visa'] = info_block.text.split('Виза')[1].split('Условия въезда')[0].strip(string.punctuation).strip() | |
country_conditions['vaccine'] = info_block.text.split('Какие вакцины признаются')[1].split('Что открыто')[0].strip(string.punctuation).strip() | |
country_conditions['conditions'] = info_block.text.split('Условия въезда')[1].split('Какие вакцины признаются')[0].strip(string.punctuation).strip() | |
return country_conditions | |
country_arr = 'Шри-Ланка' | |
print(get_info_rosturizm(country_arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment