简体   繁体   English

列表中的Python max(date)

[英]Python max(date) in list

I'm trying to return the max date in a list from a bs4 scrape. 我正在尝试从bs4抓取列表中返回最大日期。 Here's what I've got so far.. 这是到目前为止我得到的..

import requests
from datetime import date, datetime, timedelta
from collections import OrderedDict, defaultdict
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq

con = requests.get("https://au.investing.com/currencies/aud-usd-historical-data/",
               headers={'User-Agent': 'Mozilla/5.0'})
odcon = OrderedDict()
content_page = soup(con.content, 'html.parser')
table = content_page.find('table', {'class': 'genTbl closedTbl historicalTbl'})
cols = [th.text for th in table.select("th")[1:]]
for row in table.select("tbody tr"):
    data = [td.text for td in row.select("td")]
    data[0] = datetime.strptime(data[0], '%b %d, %Y').strftime('%d/%m/%Y')
    print(max(data[0]))



Output looks like this for print(data[0])
13/09/2018
12/09/2018
11/09/2018
10/09/2018
09/09/2018
07/09/2018
06/09/2018
05/09/2018
04/09/2018
03/09/2018
02/09/2018
31/08/2018
30/08/2018
29/08/2018
28/08/2018
27/08/2018
26/08/2018
24/08/2018
23/08/2018
22/08/2018
21/08/2018
20/08/2018
19/08/2018
17/08/2018
16/08/2018
15/08/2018
14/08/2018
13/08/2018

I would like the max date in this list to be returned/printed. 我希望退回/打印此列表中的最大日期。

This is probably an easy solve.. but I can't figure it out. 这可能是一个简单的解决方法。但是我无法弄清楚。 Any help would be much appreciated. 任何帮助将非常感激。

How about this mod to your code 这个mod到你的代码怎么样

dateList = []

for row in table.select("tbody tr"):
    data = [td.text for td in row.select("td")]
    d = datetime.strptime(data[0], '%b %d, %Y').date()
    dateList = dateList + [d, ]

print max(dateList)

format the date after sorting the list of the dates: 在对日期列表进行排序后格式化日期:

from bs4 import BeautifulSoup as bs4
site = requests.get("https://au.investing.com/currencies/aud-usd-historical-data/", headers={'User-Agent': 'Mozilla/5.0'})
content_page = bs4(site.content, 'html.parser')
table = content_page.find('table', {'class': 'genTbl closedTbl historicalTbl'})
cols = [th.text for th in table.select("th")[1:]]

dates = []
for row in table.select("tbody tr"):
    data = [td.text for td in row.select("td")]
    dates.append(data[0])
dates.sort()
datetime.strptime(max(dates), '%b %d, %Y').strftime('%d/%m/%Y')

output: 输出:

'13/09/2018'

BTW, cols isn't being used here. 顺便说一句,这里没有使用cols

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM