-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskin_html_page_parser.py
85 lines (70 loc) · 2.84 KB
/
skin_html_page_parser.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
from typing import List
from bs4 import BeautifulSoup
from bs4.element import Tag
class SkinHtmlPageParser():
def __init__(self, html_document: str):
"""
Initializes the SkinHtmlPageParser object.
Parameters:
- html_document (str): The HTML document for parsing.
"""
self.html_document = BeautifulSoup(html_document, 'html.parser')
self.name = self.get_name()
self.site_id = self.get_site_id()
self.float_value = self.get_float_value()
self.stickers = self.get_stickers()
self.price = self.get_price()
def get_name(self) -> str:
"""
Returns the name of the skin.
Returns:
- str: The name of the skin or 'None' if the name is not found.
"""
name = self.html_document.find('div', attrs={'class': 'skin-name'})
return name.text if name is not None else 'None'
def get_site_id(self) -> str:
"""
Returns the site ID of the skin.
Returns:
- str: The site ID of the skin or an empty string if the ID is not found.
"""
site_id = self.html_document.find('a', attrs={'class': 'market-view-in-game-link'})
site_id = site_id.get('data-id') if isinstance(site_id, Tag) else site_id
return str(site_id)
def get_float_value(self) -> float:
"""
Returns the float value of the skin.
Returns:
- float: The float value of the skin or 0.0 if the value is not found.
"""
float_value = ''.join([
element.find('div', attrs={'class': 'spec-value'}).text
for element in self.html_document.find_all('div', attrs={'class': 'spec-item'})
if element.find('div', attrs={'class': 'spec-title'}).text == 'Float'
])
return float(float_value)
def get_stickers(self) -> List[str]:
"""
Returns the list of stickers on the skin.
Returns:
- List[str]: The list of stickers on the skin or an empty list if no stickers are found.
"""
stickers_list_block = self.html_document.find('div', attrs={'class': 'sticker-list'})
if not isinstance(stickers_list_block, Tag):
stickers = []
else:
stickers = [
element['title']
for element in stickers_list_block.find_all('div', attrs={'class': 'sticker'})
]
return stickers
def get_price(self) -> float:
"""
Returns the price of the skin.
Returns:
- float: The price of the skin or 0.0 if the price is not found.
"""
price = self.html_document.find('div', attrs={'class': 'min-price-value'})
price = price.text.replace('$', '') if price is not None else 0.0
return float(price) if not isinstance(price, float) else price