Home > Back-end >  Unable to do web scraping from URL using Python Alchemy
Unable to do web scraping from URL using Python Alchemy

Time:08-22

I have a script where I'm trying to web scraping the data into table. But I'm getting errors

  raise exc.with_traceback(traceback)
ValueError: No tables found

Script :

import pandas as pd
import logging
from sqlalchemy import create engine
from urlib.parse import quote

db_connection = {mysql}://{username}:{quote'pwd'}@{DB:port}
ds_connection = create_engine(db_connection)
a = pd.read_html("https://www.centralbank.ae/en/forex-eibor/exchange-rates/")
df = pd.Dataframe(a[0])
df_final = df.loc[:,['Currency','Rate']]
df_final.to_sql('rate_table',db_connection,if_exists = append,index=false)

Can anyone suggest on this

CodePudding user response:

One easy way to obtain those exchange rates would be to scrape the API accessed to retrieve information in page (check Dev Tools - network tab):

import pandas as pd
import requests
from bs4 import BeautifulSoup

headers = {'Accept-Language': 'en-US,en;q=0.9',
          'Referer': 'https://www.centralbank.ae/en/forex-eibor/exchange-rates/'

          }
r = requests.post('https://www.centralbank.ae/umbraco/Surface/Exchange/GetExchangeRateAllCurrency', headers=headers)
dfs = pd.read_html(r.text)
print(dfs[0].loc[:,['Currency','Rates']])

This returns:

Currency Rates
0 US Dollar 3.6725
1 Argentine Peso 0.026993
2 Australian Dollar 2.52753
3 Bangladesh Taka 0.038508
4 Bahrani Dinar 9.74293
5 Brunei Dollar 2.64095
6 Brazilian Real 0.706549
7 Botswana Pula 0.287552
8 Belarus Rouble 1.45526
9 Canadian Dollar 2.82565
10 Swiss Franc 3.83311
11 Chilean Peso 0.003884
12 Chinese Yuan - Offshore 0.536978
13 Chinese Yuan 0.538829
14 Colombian Peso 0.000832
15 Czech Koruna 0.149763
16 Danish Krone 0.496304
17 Algerian Dinar 0.025944
18 Egypt Pound 0.191775
19 Euro 3.69096
20 GB Pound 4.34256
21 Hongkong Dollar 0.468079
22 Hungarian Forint 0.009112
23 Indonesia Rupiah 0.000248
24 Indian Rupee 0.045976
25 Iceland Krona 0.026232
26 Jordan Dinar 5.17472
27 Japanese Yen 0.026818
28 Kenya Shilling 0.030681
29 Korean Won 0.002746
30 Kuwaiti Dinar 11.9423
31 Kazakhstan Tenge 0.007704
32 Lebanon Pound 0.002418
33 Sri Lanka Rupee 0.010201
34 Moroccan Dirham 0.353346
35 Macedonia Denar 0.059901
36 Mexican Peso 0.181874
37 Malaysia Ringgit 0.820395
38 Nigerian Naira 0.008737
39 Norwegian Krone 0.37486
40 NewZealand Dollar 2.27287
41 Omani Rial 9.53921
42 Peru Sol 0.952659
43 Philippine Piso 0.065562
44 Pakistan Rupee 0.017077
45 Polish Zloty 0.777446
46 Qatari Riyal 1.00254
47 Serbian Dinar 0.031445
48 Russia Rouble 0.06178
49 Saudi Riyal 0.977847
50 Sudanese Pound 0.006479
51 Swedish Krona 0.347245
52 Singapore Dollar 2.64038
53 Thai Baht 0.102612
54 Tunisian Dinar 1.1505
55 Turkish Lira 0.20272
56 Trin Tob Dollar 0.541411
57 Taiwan Dollar 0.121961
58 Tanzania Shilling 0.001575
59 Uganda Shilling 0.000959
60 Vietnam Dong 0.000157
61 Yemen Rial 0.01468
62 South Africa Rand 0.216405
63 Zambian Kwacha 0.227752
64 Azerbaijan manat 2.16157
65 Bulgarian lev 1.8873
66 Croatian kuna 0.491344
67 Ethiopian birr 0.069656
68 Iraqi dinar 0.002516
69 Israeli new shekel 1.12309
70 Libyan dinar 0.752115
71 Mauritian rupee 0.079837
72 Romanian leu 0.755612
73 Syrian pound 0.001462
74 Turkmen manat 1.05079
75 Uzbekistani som 0.000336
  • Related