對於取得文章時間排序感到疑惑,是否能以日期排序
2020/06/25 04:17 下午
Python網路爬蟲討論版
鍾潤軒
觀看數:7
回答數:3
收藏數:1
pycrawler
pycrawler-d13
老師你好,作業中第一題當我對時間進行排序,為什麼得到最新的文章是前一天晚上23:59分的文章,而不是當天新的文章。
若我想先依照日期再依照時間進行排序又該如何寫呢?下面為我目前的程式碼。
for d in soup.find_all(class_="title"):
try:
NBAnew={}
NBAnew['標題']=d.text.replace('\t', '').replace('\n', '')
r = BeautifulSoup(requests.get('https://www.ptt.cc'+d.find('a')['href']).text, "html5lib")
NBAnew['作者']=r.find(class_='article-meta-value').text
NBAnew['時間']=r.find_all(class_='article-meta-value')[-1].text
NBAnews.append(NBAnew)
except:
continue
NBAnews=sorted(NBAnews, key=lambda f: f['時間'], reverse=True)
print(NBAnews[0])