-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_jt.py
62 lines (48 loc) · 2.47 KB
/
parser_jt.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
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
JT RSS-voo sisendite parsimine
"""
import makereq
import parsers_common
def getArticleListsFromHtml(pageTree, domain, maxPageURLstoVisit):
"""
Meetod uudistesaidi kõigi uudiste nimekirja loomiseks
"""
articleDescriptions = []
articleIds = []
articleImages = []
articlePubDates = []
articleTitles = pageTree.xpath('//ul[@class="list search-items-list"]/li/span/a/text()')
articleUrls = pageTree.xpath('//ul[@class="list search-items-list"]/li/span/a/@href')
articlePubDatesRaw = pageTree.xpath('//ul[@class="list search-items-list"]/li/div/span[2]/text()')
get_article_bodies = True
for i in range(0, len(articleUrls)):
articleUrl = articleUrls[i]
# get unique id from ArticleUrl
articleIds.append(articleUrl.split('/')[-2])
if (get_article_bodies is True and i < maxPageURLstoVisit):
# load article into tree
articleTree = makereq.getArticleData(articleUrl)
# description
curArtDescParent1 = parsers_common.treeExtract(articleTree, '//main/article/div[@class="flex flex--align-items-stretch"]//section') # as a parent
curArtDescParent2 = parsers_common.treeExtract(articleTree, '//main/div[@class="wrap"]/div[@class="flex flex--align-items-stretch"]//section') # as a parent
curArtDescChilds1 = parsers_common.stringify_children(curArtDescParent1)
curArtDescChilds2 = parsers_common.stringify_children(curArtDescParent2)
articleDescriptions.append(curArtDescChilds1 + ' ' + curArtDescChilds2)
# image
curArtImg = parsers_common.treeExtract(articleTree, '//main/article/div[@class="flex flex--align-items-stretch"]//figure/img[1]/@src') or "//"
curArtImg = "http:" + curArtImg
articleImages.append(curArtImg)
# timeformat magic from "24. detsember 2017 17:51" to datetime()
curArtPubDate = articlePubDatesRaw[i]
curArtPubDate = parsers_common.longMonthsToNumber(curArtPubDate)
curArtPubDate = parsers_common.rawToDatetime(curArtPubDate, "%d. %m %Y %H:%M")
articlePubDates.append(curArtPubDate)
return {"articleDescriptions": articleDescriptions,
"articleIds": articleIds,
"articleImages": articleImages,
"articlePubDates": articlePubDates,
"articleTitles": articleTitles,
"articleUrls": articleUrls,
}