References
This module providing class represent a session in scrapping process.
This module is used for scrape financial statement data from Yahoo Finance. In one session, you can only scrape financial statement data from one company. The data consist of balance sheet, income statement, and cashflow statement. Those are tabulated for each statement. You can also get one table that contain selected features from each statement, and one table that contain selected financial metrics from the selected features.
This module require selenium and beautifoul soup 4 for scrapping and crawling, pandas for making dataframe, and numpy for numerical manipulation. Please install required library before use it!
YFinanceScrapper
A class that represent one scrape session
Examples:
>>> bca=YFinanceScrapper('BBCA.JK')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
company_code |
str
|
The company code that you want to scrape |
required |
Attributes:
Name | Type | Description |
---|---|---|
address |
dict
|
Dictionary that contain statement as key and yahoo adress as value. |
balance_sheet |
pandas.Dataframe
|
A pandas Dataframe that contain balance sheet statement data. |
cash_flow |
pandas.Dataframe
|
A pandas Dataframe that contain cash flow statement data. |
company_code |
str
|
The company code that you want to scrape. |
collect |
list
|
A list that contain all name of features and their value. |
content |
bs4.BeautifulSoup
|
BeautifulSoup object that contain Yahoo Finance HTML file in Pythonic idioms. |
features |
list
|
A list that contain name of features those are collected. |
headers |
list
|
A list that contain name of headers in Yahoo Finance financial statements table. |
imp_dataframe |
pandas.Dataframe
|
A pandas Dataframe that contain selected features from each statement. |
income_statement |
pandas.Dataframe
|
A pandas Dataframe that contain income statement data. |
metric |
pandas.Dataframe
|
A pandas Dataframe that contain selected financial metrics from selected features. |
note |
list
|
A list that contain note that explain value (Ex:Currency). |
path |
str
|
Location of chromedriver. |
table_choice |
list
|
List of statement that can be chosen. |
time |
list
|
List of periodic of collected data. |
Source code in scrape/ScraFSY.py
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
convert_to_csv(name_of_table)
Convert selected statements table to csv.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> df= bca.get_finance_data('Income Statement')
>>> bca.convert_to_csv(income_statement)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name_of_table |
str
|
Name of table. |
required |
Returns:
Type | Description |
---|---|
csv file of selected dataframe. |
Source code in scrape/ScraFSY.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
|
create_dataframe(collect, headers, time, statement)
Create dataframe from data that already collected from parsing process.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> df= bca.create_dataframe(self.collect,self.headers,self.time,'Income Statement')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collect |
list
|
A list that contain all name of features and their value. |
required |
headers |
list
|
A list that contain name of headers in Yahoo Finance financial statements table. |
required |
time |
list
|
List of periodic of collected data. |
required |
statement |
str
|
The selected statement that is gonna be scraped. |
required |
Returns:
Name | Type | Description |
---|---|---|
df2 |
pandas.Dataframe
|
Dataframe that contain data from selected statement. |
Source code in scrape/ScraFSY.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
get_alldata(param1=None)
Retrieve all statement in 3 dataframe of selected company.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> bca.get_alldata()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
param1 |
obj: |
None
|
Returns:
Name | Type | Description |
---|---|---|
balance_sheet |
pandas.Dataframe
|
A pandas Dataframe that contain balance sheet statement data. |
cash_flow |
pandas.Dataframe
|
A pandas Dataframe that contain cash flow statement data. |
income_statement |
pandas.Dataframe
|
A pandas Dataframe that contain income statement data. |
Source code in scrape/ScraFSY.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
get_finance_data(statement)
Retrieve dataframe that contains all data in selected statement.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> df= bca.get_finance_data('Income Statement')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
statement |
str
|
The selected statement that is gonna be scraped. |
required |
Returns:
Name | Type | Description |
---|---|---|
balance_sheet |
pandas.Dataframe
|
A pandas Dataframe that contain balance sheet statement data. |
cash_flow |
pandas.Dataframe
|
A pandas Dataframe that contain cash flow statement data. |
income_statement |
pandas.Dataframe
|
A pandas Dataframe that contain income statement data. |
Source code in scrape/ScraFSY.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
get_html_data(statement)
Retrieve a Beautifulsoup object that contains JSON file from Yahoo Finance.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> content= bca.get_html_data('Income Statement')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
statement |
str
|
The selected statement that is gonna be scraped. |
required |
Returns:
Name | Type | Description |
---|---|---|
content |
bs4.BeautifulSoup
|
BeautifulSoup object that contain Yahoo Finance HTML file in Pythonic idioms. |
Source code in scrape/ScraFSY.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
important_dataframe(param1=None)
Create dataframe that contain selected features from all statements table.
Examples:
>>> goto = YFinanceScrapper('GOTO.JK')
>>> goto.get_alldata()
>>> goto.important_dataframe()
Args:
param1 (:obj:str
, optional): The first parameter.
Defaults to None.
Returns:
Name | Type | Description |
---|---|---|
imp_dataframe |
pandas.Dataframe
|
A pandas Dataframe that contain selected features from each statement. |
Source code in scrape/ScraFSY.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
metric_dataframe(param1=None)
Create dataframe that contain selected financial metrics from important dataframe.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> bca.get_alldata()
>>> bca.important_dataframe()
>>> bca.metric_dataframe()
Args:
param1 (:obj:str
, optional): The first parameter.
Defaults to None.
Returns:
Name | Type | Description |
---|---|---|
metric |
pandas.Dataframe
|
A pandas Dataframe that contain selected financial metrics from selected features. |
Source code in scrape/ScraFSY.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
parse_data(content, statement)
Parse JSON file from Yahoo Finance to get data in Financial Statements.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> collect= bca.parse_data(self.content,'Income Statement')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
bs4.BeautifulSoup
|
BeautifulSoup object that contain Yahoo Finance HTML file in Pythonic idioms. |
required |
statement |
str
|
The selected statement that is gonna be scraped. |
required |
Returns:
Name | Type | Description |
---|---|---|
collect |
list
|
A list that contain all name of features and their value. |
headers |
list
|
A list that contain name of headers in the table. |
time |
list
|
List of periodic of collected data. |
Source code in scrape/ScraFSY.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
reset_data(param1=None)
Reset variable content, features, collect, time, and headers so it can use for scrape again.
Examples:
>>> bca = YFinanceScrapper('BBCA.JK')
>>> df= bca.get_finance_data('Income Statement')
>>> bca.reset_data()
>>> df_new= bca.get_finance_data('Balance Sheet')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
param1 |
obj: |
None
|
Returns:
Name | Type | Description |
---|---|---|
collect |
list
|
Empty list for store collected data. |
content |
NoneType
|
Variable content set to None for collected BeautifulSoup JSON data. |
features |
list
|
Set features to default for storing features. |
headeres |
list
|
Empty list for storing headers of financial statements table headers. |
time |
list
|
Empty list for storing periodic of financial statements data. |
Source code in scrape/ScraFSY.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
value_to_num(x)
Data manipulation that convert '-' to nan, convert ',' to '', convert 'K' to 1000.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
optional
|
Selected value that want to change. |
required |
Returns:
Name | Type | Description |
---|---|---|
x |
float
|
New value with type float. |
Source code in scrape/ScraFSY.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|