【问题标题】:Extract unix timestamp from <tag datetime="unix-timestamp">从 <tag datetime="unix-timestamp"> 中提取 unix 时间戳
【发布时间】:2020-12-16 16:18:52
【问题描述】:

我试图从 html 源代码中获取多个 unix 时间戳,我可以将其转换为 yyyy-mm-dd,然后将其传递给谷歌日历以进行活动。我似乎无法从&lt;tag datetime=""&gt; 获得 unix 时间戳。这个之前的标签是&lt;div class="matchlist__match__column--details"&gt;

soup.find_all('time') 告诉我我需要的一切都在那里,我可以使用soup.find('time', {'datetime': True})['datetime'] 获得第一个 unix 时间戳。我也可以使用get_text(),但是字符串写成d. month - hh:mm,如果我想在google日历中使用它就不好了。

我有一些有希望的线索,但我不断收到错误消息,我的搜索结果没有结果,或者我可能遗漏了一些明显的东西。我已经从零开始自学了两个月,但我还是个初学者,欢迎对我的方法提出任何意见。

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.
 

【问题讨论】:

    标签: python beautifulsoup tags


    【解决方案1】:
    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")
    
    

    输出:

    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
    

    【讨论】:

    • 谢谢!非常感谢。祝你周末愉快。
    • 如果我可以问你一个跟进:我如何获得该网站正在使用的这个 API 的文档?我想提取一个特定的团队名称,但无法弄清楚。
    • @AndletYo 您正在处理JSON dict,因此您可以使用print(data.keys()) 来获取您要查找的内容。还要检查that
    猜你喜欢
    • 2010-11-19
    • 2018-01-04
    • 2011-12-10
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多