-
Notifications
You must be signed in to change notification settings - Fork 0
/
xpi.py
98 lines (77 loc) · 2.76 KB
/
xpi.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
86
87
88
89
90
91
92
93
94
95
96
97
98
from brdata.core.crawler import Crawler
import pandas as pd
from brdata.core import exceptions
class XPICrawler(Crawler):
"""Crawler for XP Investimentos.
Examples:
```python
import brdata
crawler = brdata.XPICrawler()
crawler.get_analysis("PETR4")
```
"""
def __init__(self):
super().__init__("https://conteudos.xpi.com.br/")
@staticmethod
def _analysis_page_to_pandas(page) -> pd.Series:
"""Prepara o pd.Series com os dados da ação a partir do conteúdo da página 'content' informado."""
to_numeric_cols = [
"Preço Atual",
"Preço de Entrada",
"Primeiro Objetivo",
"Objetivo Final",
"Stop Loss",
"Preço Alvo",
"Potencial",
"Risco (0 - 100)",
]
product_data = page.find_all("li", {"class": "item-dado-produto"})
if len(product_data) == 0:
return None
data = {}
for li in product_data:
spans = li.find_all("span")
col = spans[0].text
val = spans[1].text if len(spans) > 1 else li.contents[2]
val = (
val.replace("\n", "")
.replace(",", ".")
.replace("R$", "")
.replace("%", "")
.replace("%", "")
.split(" ")
)
while "" in val:
val.remove("")
val = val[0]
if val == "-":
val = None
elif col in to_numeric_cols:
val = float(val)
elif col == "Potencial":
val = float(val) / 100
data[col] = val
return pd.Series(data)
def get_analysis(
self, code: str, to_pandas: bool = True, enable_cache: bool = True, **kwargs
) -> pd.Series:
"""Get stock analysis from XP Investimentos.
Args:
code (str): Stock code.
to_pandas (bool, optional): Whether to return a pandas.Series or a dict. Defaults to True.
enable_cache (bool, optional): Whether to enable cache. Defaults to True.
Raises:
exceptions.NotFoundException: Stock not found.
Returns:
pd.Series: Stock analysis.
"""
try:
page = super().get_page_soup(
path=f"/acoes/{code.lower()}", enable_cache=enable_cache, **kwargs
)
except exceptions.RequestException as e:
raise exceptions.NotFoundException(f"[XPI] Stock {code} not found.") from e
output = self._analysis_page_to_pandas(page)
if output is None:
raise exceptions.NotFoundException(f"[XPI] Stock {code} not found.")
return output if to_pandas else output.to_dict()