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.

120 lines
4.5 KiB

  1. import yaml
  2. import json
  3. import urllib.request, urllib.error, urllib.parse
  4. from lxml import etree
  5. import lxml.html
  6. import lxml.html.soupparser
  7. class membersParliamentCrawler(object):
  8. def __init__(self, configFile):
  9. with open(configFile, "r") as stream:
  10. try:
  11. self.config = yaml.safe_load(stream)
  12. except yaml.YAMLError as exc:
  13. print(exc)
  14. # input list of countries in form of ['nicaragua', 'honduras', .. , 'mexico']
  15. def downloadMemberListPagesOfCountries(self, listOfCountries):
  16. # download only html pages of the countries specified in input
  17. for country in listOfCountries:
  18. for key in self.config:
  19. if key in listOfCountries:
  20. try:
  21. memberList = self.config.get(key).get('memberList')
  22. except Exception as e:
  23. print("There is a problem with the entry memberList in the config.yaml - the original error message is:", e)
  24. try:
  25. memberListLink = memberList.get('link')
  26. except Exception as e:
  27. print("No memberListLink defined in config.yaml - the original error message is:", e)
  28. # download the html page of the List of Members
  29. response = urllib.request.urlopen(memberListLink)
  30. webContent = response.read().decode('UTF-8')
  31. # save interim results to files
  32. f = open('pages/' + key +'MemberList.html', 'w+')
  33. f.write(webContent)
  34. f.close
  35. def parseMemberListData2dictionary(self, listOfCountries):
  36. for country in listOfCountries:
  37. try:
  38. #use soupparser to handle broken html
  39. tree = lxml.html.soupparser.parse('pages/' + country + 'MemberList.html')
  40. # for e in tree.iter():
  41. #
  42. # print(e.tag)
  43. #
  44. # for e in tree.xpath('//html//body//form//table//tr//td//table//tr'):
  45. #
  46. # #print(etree.tostring(e).decode())
  47. dictionaryMemberList = {}
  48. countryConf = self.config.get(country)
  49. countryConfMemberList = countryConf.get('memberList')
  50. countryConfMemberListParent = countryConfMemberList.get('parent')
  51. countryConfMemberListChildName = countryConfMemberList.get('child-name')
  52. countryConfMemberListChildLink = countryConfMemberList.get('child-link')
  53. for n in range(len(tree.xpath(countryConfMemberListParent))):
  54. name = tree.xpath(countryConfMemberListParent + '[' + str(n) + ']' + countryConfMemberListChildName)
  55. link = tree.xpath(countryConfMemberListParent + '[' + str(n) + ']' + countryConfMemberListChildLink)
  56. if len(name) > 0:
  57. dictionaryMemberList[name[0]] = {}
  58. dictionaryMemberList[name[0]]['name'] = name[0]
  59. dictionaryMemberList[name[0]]['link'] = link[0]
  60. except Exception as e:
  61. print('parsing the html did not work. Possibly you first have to downloadMemberListPagesOfCountries(). The original error message is:', e)
  62. # save interim results to files
  63. f = open('output/' + country +'MemberList.txt', 'w+')
  64. f.write(str(dictionaryMemberList))
  65. f.close
  66. def parseMemberData2dictionary(self, listOfCountries):
  67. for country in listOfCountries:
  68. f = open('output/' + country +'MemberList.txt')
  69. text = f.read()
  70. # replace quotes with double quotes because of JSON specification - RFC7159 which would result in error for json.loads function
  71. text = text.replace("\'", "\"")
  72. dictionaryMemberList = json.loads(text)
  73. for member in dictionaryMemberList:
  74. print('oi')
  75. print(dictionaryMemberList[member]['link'])