automated Pipeline for parsing profiles of politically exposed persons (PEP) into Wikidata
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.9 KiB

  1. import os
  2. import yaml
  3. import json
  4. import urllib.request, urllib.error, urllib.parse
  5. from lxml import etree
  6. import lxml.html
  7. import lxml.html.soupparser
  8. class membersParliamentCrawler(object):
  9. def __init__(self, configFile):
  10. with open(configFile, "r") as stream:
  11. try:
  12. self.config = yaml.safe_load(stream)
  13. except yaml.YAMLError as exc:
  14. print(exc)
  15. # input list of countries in form of ['nicaragua', 'honduras', .. , 'mexico']
  16. def downloadMemberListPagesOfCountries(self, listOfCountries):
  17. # download only html pages of the countries specified in input
  18. for country in listOfCountries:
  19. for key in self.config:
  20. if key in listOfCountries:
  21. try:
  22. memberList = self.config.get(key).get("memberList")
  23. except Exception as e:
  24. print(
  25. "There is a problem with the entry memberList in the config.yaml - the original error message is:",
  26. e,
  27. )
  28. try:
  29. memberListLink = memberList.get("link")
  30. except Exception as e:
  31. print(
  32. "No memberListLink defined in config.yaml - the original error message is:",
  33. e,
  34. )
  35. # download the html page of the List of Members
  36. response = urllib.request.urlopen(memberListLink)
  37. webContent = response.read().decode("UTF-8")
  38. # save interim results to files
  39. f = open("crawlers/pages/" + key + "MemberList.html", "w+")
  40. f.write(webContent)
  41. f.close
  42. def parseMemberListData2dictionary(self, listOfCountries):
  43. for country in listOfCountries:
  44. try:
  45. # use soupparser to handle broken html
  46. tree = lxml.html.soupparser.parse(
  47. "crawlers/pages/" + country + "MemberList.html"
  48. )
  49. # for e in tree.iter():
  50. #
  51. # print(e.tag)
  52. #
  53. # for e in tree.xpath('//html//body//form//table//tr//td//table//tr'):
  54. #
  55. # #print(etree.tostring(e).decode())
  56. dictionaryMemberList = {}
  57. countryConf = self.config.get(country)
  58. countryDomain = countryConf.get("domain")
  59. countryConfMemberList = countryConf.get("memberList")
  60. countryConfMemberListParent = countryConfMemberList.get("parent")
  61. countryConfMemberListChildName = countryConfMemberList.get("child-name")
  62. countryConfMemberListChildLink = countryConfMemberList.get("child-link")
  63. for n in range(len(tree.xpath(countryConfMemberListParent))):
  64. name = tree.xpath(
  65. countryConfMemberListParent
  66. + "["
  67. + str(n)
  68. + "]"
  69. + countryConfMemberListChildName
  70. )
  71. link = tree.xpath(
  72. countryConfMemberListParent
  73. + "["
  74. + str(n)
  75. + "]"
  76. + countryConfMemberListChildLink
  77. )
  78. if len(name) > 0:
  79. dictionaryMemberList[n] = {}
  80. dictionaryMemberList[n]["name"] = name[0]
  81. if countryDomain in link[0]:
  82. dictionaryMemberList[n]["link"] = link[0]
  83. if countryDomain not in link[0]:
  84. dictionaryMemberList[n]["link"] = countryDomain + link[0]
  85. except Exception as e:
  86. print(
  87. "parsing the html did not work. Possibly you first have to downloadMemberListPagesOfCountries(). The original error message is:",
  88. e,
  89. )
  90. # save interim results to files
  91. f = open("crawlers/output/" + country + "MemberList.txt", "w+")
  92. f.write(str(dictionaryMemberList))
  93. f.close
  94. def downloadMemberDataHtmls(self, listOfCountries):
  95. for country in listOfCountries:
  96. f = open("crawlers/output/" + country + "MemberList.txt")
  97. text = f.read()
  98. dictionaryMemberList = eval(text)
  99. for memberid in dictionaryMemberList:
  100. memberLink = dictionaryMemberList[memberid]["link"]
  101. # download the html page of the Member
  102. response = urllib.request.urlopen(memberLink)
  103. webContent = response.read().decode("UTF-8")
  104. # save interim results to files
  105. filename = "crawlers/pages/" + country + "/" + str(memberid) + ".html"
  106. os.makedirs(os.path.dirname(filename), exist_ok=True)
  107. f = open(filename, "w+")
  108. f.write(webContent)
  109. f.close
  110. def parseMemberData2dictionary(self, listOfCountries):
  111. for country in listOfCountries:
  112. print("started to parse data of member of " + country + " ..")
  113. f = open("crawlers/output/" + country + "MemberList.txt")
  114. text = f.read()
  115. dictionaryMemberList = eval(text)
  116. countryConf = self.config.get(country)
  117. countryDomain = countryConf.get("domain")
  118. countryConfMember = countryConf.get("member")
  119. countryConfMemberInfo1 = countryConfMember.get("info-1")
  120. countryConfMemberInfo1Parent = countryConfMemberInfo1.get("parent")
  121. countryConfMemberInfo1ChildPoliticalParty = countryConfMemberInfo1.get(
  122. "child-politicalParty"
  123. )
  124. for memberid in dictionaryMemberList:
  125. print(
  126. "started to parse data of member with name "
  127. + dictionaryMemberList[memberid]["name"]
  128. + " .."
  129. )
  130. filename = "crawlers/pages/" + country + "/" + str(memberid) + ".html"
  131. tree = lxml.html.soupparser.parse(filename)
  132. politicalParty = tree.xpath(
  133. countryConfMemberInfo1Parent
  134. + countryConfMemberInfo1ChildPoliticalParty
  135. )
  136. print("oi", politicalParty)
  137. if len(politicalParty) > 0:
  138. dictionaryMemberList[memberid]["political party"] = politicalParty[
  139. 0
  140. ]
  141. f = open("crawlers/output/" + country + "MemberList.txt", "w+")
  142. f.write(str(dictionaryMemberList))
  143. f.close