0

Im trying to get multiple unix timestamp from a html source code that I can convert to yyyy-mm-dd and then later pass it to google calendar to make events. I can't seem to get the unix timestamp from <tag datetime="">. The tag before this one is <div class="matchlist__match__column--details">

soup.find_all('time') shows me that everything I need is there and I can get the first unix timestamp with soup.find('time', {'datetime': True})['datetime']. I can also use get_text(), but the string is written d. month - hh:mm, which is no good if I want it in google calendar.

I had some promising leads but i keep getting error messages and my search results have not been fruitful or maybe im missing something obvious. I've teached myself from scratch for two months and im still a beginner any input on my methods are welcome.

soup.find_all('time', datetime = True)['datetime'] TypeError: list indices must be integers or slices, not str 
# The website im collecting data from will announce # the game dates for next season soon and i # want to help a friend of mine who manages one # of the team as a way to teach myself python. # Note that the webpage here is a placeholder # and the dates are in the past. from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Options DRIVER_PATH = 'C:/folder/chromedriver.exe' options = Options() options.headless = True options.add_argument("--window-size=1920,1200") driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH) driver.get("https://www.telialigaen.no/rl/resultater?division=6221&season=6091") soup = BeautifulSoup(driver.page_source, 'html.parser') homeTeam = [] for el in soup.find_all('strong', attrs={'class': 'mr-2'}): homeTeam.append(el.get_text()) awayTeam = [] for el in soup.find_all('strong', attrs={'class': 'ml-2'}): awayTeam.append(el.get_text()) date = [] # i dunno # The goal is to have all teams and dates in nice # separate lists. # From here I will pick myTeam and create google # calendar events the day they are playing. 

1 Answer 1

0
import requests import csv headers = { "referrer": "https://www.telialigaen.no/rl/resultater?division=6221&season=6091" } def main(url): r = requests.get(url, headers=headers).json() with open("data.csv", 'w', newline="") as f: writer = csv.writer(f) writer.writerow(["Home Team", "Date", "Away Team"]) for data in r: writer.writerow([data['homeTeam']['name'], data['startTime'], data['awayTeam']['name']]) main("https://www.telialigaen.no/api/matches?division=6221&game=rl&limit=100&offset=0&order=roundNumber&season=6091&status=finished") 

Output:

Home Team,Date,Away Team PuttaGutta,2019-11-03 19:00:00,Domino Esport UnTouchables,2019-11-03 19:00:00,Electric Taco For En Tragedie,2019-10-31 20:00:00,Smerte UnTouchables,2019-10-24 21:00:00,Domino Esport Electric Taco,2019-10-24 19:00:00,Smerte For En Tragedie,2019-10-24 19:00:00,Team SYRE Smerte,2019-10-17 20:00:00,Domino Esport PuttaGutta,2019-10-17 20:00:00,Ultra Handicap Electric Taco,2019-10-15 19:00:00,Team SYRE UnTouchables,2019-10-17 19:00:00,PuttaGutta Domino Esport,2019-10-17 19:00:00,Team SYRE Ultra Handicap,2019-10-17 19:00:00,For En Tragedie UnTouchables,2019-09-30 19:30:00,Ultra Handicap Electric Taco,2019-09-27 20:00:00,For En Tragedie Smerte,2019-09-26 19:00:00,PuttaGutta Team SYRE,2019-09-20 19:00:00,PuttaGutta Smerte,2019-09-19 20:00:00,UnTouchables Domino Esport,2019-09-19 19:00:00,For En Tragedie Ultra Handicap,2019-09-19 18:00:00,Electric Taco Smerte,2019-09-15 21:00:00,Ultra Handicap Team SYRE,2019-09-13 18:30:00,UnTouchables Domino Esport,2019-09-11 19:00:00,Electric Taco PuttaGutta,2019-09-09 21:00:00,For En Tragedie Team SYRE,2019-09-05 19:00:00,Smerte Ultra Handicap,2019-09-05 19:00:00,Domino Esport PuttaGutta,2019-08-29 21:00:00,Electric Taco For En Tragedie,2019-08-29 19:00:00,UnTouchables Team SYRE,2019-08-29 19:00:00,Ultra Handicap 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! That is much appreciated. Wish you a nice weekend.
If I can ask you a follow up: How do I get the docs for this API the site is using? I would like to extrace one teamName spesific, but can't figure it out.
@AndletYo you are dealing with JSON dict so you can just use print(data.keys()) to pickup what you looking for it. also check that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.