def scrape_page(soup):
for i in range(len(soup.tbody.findAll('tr'))):
# Alert time
alert_time = soup.tbody.findAll('tr')[i].find(class_ = "warframe-countdown")['data-source']
alert_time = str(datetime.strptime(alert_time, "%Y-%m-%dT%H:%M:%S+%f"))
# Alert destination
destination = "".join(soup.tbody.findAll('tr')[i].find(class_ = "alternative").stripped_strings)
# Split by space followed by ( or ending with )
alert_destination = re.split(' \(|\)', destination)[0]
alert_destination_planet = re.split(' \(|\)', destination)[1]
# Check if there is a credit reward
if ("".join(soup.tbody.findAll('tr')[i].findAll('td')[1].stripped_strings) == "?"):
alert_credits = None
else:
alert_credits = int("".join(soup.tbody.findAll('tr')[i].findAll('td')[1].stripped_strings).replace(",", ""))
# Check if there is loot
if (soup.tbody.findAll('tr')[i].findAll('td')[2].find(class_ = "tag")):
alert_loot_type = soup.tbody.findAll('tr')[i].findAll('td')[2].find(class_ = "tag").get_text()
else:
alert_loot_type = None
if (soup.tbody.findAll('tr')[i].findAll('td')[2].find(class_ = "alternative")):
alert_loot = "".join(soup.tbody.findAll('tr')[i].findAll('td')[2].find(class_ = "alternative").stripped_strings)
else:
alert_loot = None
# Insert into MySQL database 'warframe', table 'alerts'
try:
x.execute(
"""
INSERT INTO alerts (time, destination, destination_planet, credits, loot_type, loot)
VALUES (%s, %s, %s, %s, %s, %s)
""", [alert_time, alert_destination, alert_destination_planet, alert_credits, alert_loot_type, alert_loot])
conn.commit()
except:
conn.rollback()