-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
67 lines (54 loc) · 2.19 KB
/
util.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os, sys, bs4
from bs4 import BeautifulSoup
from datetime import datetime
from selenium import webdriver
from selenium.common import exceptions
APP_DIR = os.path.dirname(os.path.dirname('__filename__'))
LOG_FILE = open('log.txt', 'w')
def get_text(element):
if element is None:
return ''
if type(element) == bs4.element.Tag:
return element.text or ''
return str(element) or ''
def print_log(*args, **kwargs):
cur_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f'[{cur_time}]', *args, **kwargs)
args_str = ' '.join([str(arg) for arg in args])
LOG_FILE.write(f'[{cur_time}] {args_str}\n')
class Chrome:
_chrome_driver = os.path.join(APP_DIR, 'chromedriver')
_chrome_options = webdriver.ChromeOptions()
_driver = None
def __init__(self, headless=False):
if headless:
self._chrome_options.add_argument('--headless')
self._chrome_options.add_argument('--no-sandbox')
self._chrome_options.add_argument('--disable-dev-shm-usage')
self._chrome_options.add_argument('--disable-gpu')
self._chrome_options.add_argument('--window-size=1600,900')
self._chrome_options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36')
self._driver = self.create_driver()
def create_driver(self):
driver = webdriver.Chrome(self._chrome_driver, options=self._chrome_options)
driver.implicitly_wait(3)
return driver
def get_driver(self):
return self._driver
def get(self, url):
self.handle_exceptions()
self._driver.get(url)
def parse_html(self):
self.handle_exceptions()
html = self._driver.page_source
soup = BeautifulSoup(html, 'html.parser')
return soup
def handle_exceptions(self):
try:
_ = self._driver.window_handles
except (exceptions.WebDriverException, exceptions.NoSuchWindowException) as e:
print_log('[ERROR]', e, file=sys.stderr)
self._driver.quit()
raise SystemExit
sys.exit(0)
return False