-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_lounaeestlane.py
52 lines (40 loc) · 1.93 KB
/
parser_lounaeestlane.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Lõunaeestlane RSS-voo sisendite parsimine
"""
import makereq
import parsers_common
def getArticleListsFromHtml(pageTree, domain, maxPageURLstoVisit):
"""
Meetod uudistesaidi kõigi uudiste nimekirja loomiseks
"""
articleDescriptions = []
articleIds = []
articleImages = pageTree.xpath('//div[@class="col-sm-6"]/div[@class="post-item"]/a/div/img/@src')
articlePubDates = []
articleTitles = pageTree.xpath('//div[@class="col-sm-6"]/div[@class="post-item"]/a/h3/text()')
articleUrls = pageTree.xpath('//div[@class="col-sm-6"]/div[@class="post-item"]/a/@href')
get_article_bodies = False
for i in range(0, len(articleUrls)):
articleUrl = articleUrls[i]
# generate unique id from ArticleUrl
articleIds.append(parsers_common.urlToHash(articleUrl))
if (get_article_bodies is True and i < maxPageURLstoVisit):
# load article into tree
articleTree = makereq.getArticleData(articleUrl)
# get first paragraph as header
curArtDesc = parsers_common.treeExtract(articleTree, '//div[@class="col-sm-9"]/p[1]/strong/text()')
articleDescriptions.append(curArtDesc)
# timeformat magic from "Avaldatud: 14 detsember, 2017" to datetime()
curArtPubDate = parsers_common.treeExtract(articleTree, '//div[@class="col-sm-9"]/div[@class="page-header"]/em/text()')
curArtPubDate = parsers_common.longMonthsToNumber(curArtPubDate.split(':')[1])
curArtPubDate = parsers_common.rawToDatetime(curArtPubDate, "%d %m, %Y")
articlePubDates.append(curArtPubDate)
return {"articleDescriptions": articleDescriptions,
"articleIds": articleIds,
"articleImages": articleImages,
"articlePubDates": articlePubDates,
"articleTitles": articleTitles,
"articleUrls": articleUrls,
}