Answer the question
In order to leave comments, you need to log in
How to make exceptions for parsing?
I made exceptions for the parser only on ConnectionError and it does not work well when I try to check it, it just returns emptiness and not the text that I would like to see.
Yes, and I need to make exceptions for the emptiness PS when the user did not enter anything and wanted to make another exception when there is no such product on the site,
The code where the site page is formed
import requests
url = 'http://www.gadget.kg/catalog/search?q='
class GadgetExtractor:
def get_product_link(self, name_list: list) -> str: # создаю ссылку поисковика
full_url = ''
for arg in name_list:
full_url += arg
if arg != name_list[-1]:
full_url += '+'
product_link = url + full_url
return product_link
def get_html(self, url: str) -> str:
try:
res = requests.get(url=url) # здесь беру html сайта
res.raise_for_status()
return res.text
except requests.HTTPError as error:
code = error.res.status_code
print('Ошибка', code)
exit()
except ConnectionError:
print('Плохой интернет')
exit()
from bs4 import BeautifulSoup
class GadgetTransform:
def get_data(self, html: str) -> list:
soup = BeautifulSoup(html, 'html.parser')
if soup.find('div', class_='empty-cart'):
print(soup.find('div', class_='empty-cart').get_text())
exit()
items = soup.find_all('div', class_='hit__slide')
phone = []
for item in items:
price = item.find('span', class_='hit__slide__price')
if price:
price = price.get_text()
else:
price = ''
phone = [i for i in phone if i['cost'] != '']
phone.append({
'title': item.find("h6", class_='hit__slide__title').get_text(),
'cost': price,
})
return phone
from sys import argv
from .transform import GadgetTransform
from .extractor import GadgetExtractor
def main():
gadget_extractor = GadgetExtractor()
product_link = gadget_extractor.get_product_link(name_list=argv[1:])
product_page = gadget_extractor.get_html(url=product_link)
gadget_transform = GadgetTransform()
detal_of_product = gadget_transform.get_data(html=product_page)
print(detal_of_product)
if __name__ == '__main__':
main()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question