I want to scrape exchange rate data from July 1 2021 to June 30 2022 by enumerating exchangeDate variable and save it to excel.
Here is my code so far:
JavaScript
x
24
24
1
import requests
2
from bs4 import BeautifulSoup
3
import pandas as pd
4
5
# Set the URL for the website you want to scrape
6
url = "https://www.bot.go.tz/ExchangeRate/previous_rates?__RequestVerificationToken=P0qGKEy8P6ISFMLlu7mKvMi4YrMyeHc1aCz4ZuGQVyJ6mK9w6StV6QPyinF7ym_mAZG6yO6ShU1DuFm6teqBAxCcCrEQSjz7KtXzi2kbJH41&exchangeDate=04%2F05%2F2022"
7
8
# Send an HTTP request to the website and retrieve the HTML content
9
response = requests.get(url)
10
html = response.content
11
12
# Parse the HTML content using BeautifulSoup
13
soup = BeautifulSoup(html, "html.parser")
14
15
# Find the table containing the data you want to scrape
16
table = soup.find("table", attrs={"class": "table"})
17
18
# Extract the data from the table and save it to a Pandas DataFrame
19
df = pd.read_html(str(table))[0]
20
21
# Save the DataFrame to an Excel file
22
df.to_excel("exchange_Rate_data.xlsx", index=False)
23
24
How do I loop through all dates?
Advertisement
Answer
You can use something like this:
JavaScript
1
29
29
1
import requests
2
from bs4 import BeautifulSoup
3
import pandas as pd
4
5
start='2021-07-01'
6
end='2022-06-30'
7
dates=[i.replace('-','%2F') for i in pd.date_range(start,end,freq='d').strftime('%m-%d-%Y').tolist()]
8
9
final_df=pd.DataFrame()
10
for i in dates:
11
# Set the URL for the website you want to scrape
12
url = "https://www.bot.go.tz/ExchangeRate/previous_rates?__RequestVerificationToken=P0qGKEy8P6ISFMLlu7mKvMi4YrMyeHc1aCz4ZuGQVyJ6mK9w6StV6QPyinF7ym_mAZG6yO6ShU1DuFm6teqBAxCcCrEQSjz7KtXzi2kbJH41&exchangeDate={}".format(i)
13
14
# Send an HTTP request to the website and retrieve the HTML content
15
response = requests.get(url)
16
html = response.content
17
18
# Parse the HTML content using BeautifulSoup
19
soup = BeautifulSoup(html, "html.parser")
20
21
# Find the table containing the data you want to scrape
22
table = soup.find("table", attrs={"class": "table"})
23
24
# Extract the data from the table and save it to a Pandas DataFrame
25
df = pd.read_html(str(table))[0]
26
final_df=pd.concat([final_df,df])
27
28
final_df.to_excel("exchange_Rate_data.xlsx", index=False)
29