-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrapperBase.py
33 lines (28 loc) · 920 Bytes
/
ScrapperBase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests
from bs4 import BeautifulSoup
class ScrapperBase:
@staticmethod
def get_html(url):
try:
return requests.get(url, timeout=10).text
except requests.exceptions.ConnectionError:
print('URL is unavailable')
exit()
@staticmethod
def get_soup(html, parser='html.parser', tags_to_delete=None):
if tags_to_delete is None:
tags_to_delete = ()
soup = BeautifulSoup(html, parser)
for tag in tags_to_delete:
for tg in soup.find_all(tag):
tg.replaceWith('')
return soup
@staticmethod
def repeat_until_done(func):
def wrapped(*args, **kwargs):
while True:
try:
return func(*args, **kwargs)
except AttributeError:
print('Error. Trying to do it again.')
return wrapped