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.

244 lines
9.4 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 fdb_spider(object):
  9. def __init__(self, config_file):
  10. with open(config_file, "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 funding databases in form of yaml file ['foerderinfo.bund.de', 'ausschreibungen.giz.de', .. , 'usw']
  16. def download_entry_list_pages_of_funding_databases(self, list_of_fdbs):
  17. # download only html pages of the funding databases specified in input
  18. for fdb in list_of_fdbs:
  19. for key in self.config:
  20. if key in list_of_fdbs:
  21. try:
  22. entry_list = self.config.get(key).get("entry-list")
  23. except Exception as e:
  24. print(
  25. "There is a problem with the configuration variable entryList in the config.yaml - the original error message is:",
  26. e,
  27. )
  28. try:
  29. entry_list_link1 = entry_list.get("link1")
  30. except Exception as e:
  31. print(
  32. "No link1 defined in config.yaml - the original error message is:",
  33. e,
  34. )
  35. try:
  36. entry_list_link2 = entry_list.get("link2")
  37. except Exception as e:
  38. print(
  39. "No link2 defined in config.yaml - the original error message is:",
  40. e,
  41. )
  42. try:
  43. entry_iteration_var_list = eval(entry_list.get("iteration-var-list"))
  44. except Exception as e:
  45. print(
  46. "No iteration-var-list defined in config.yaml - the original error message is:",
  47. e,
  48. )
  49. for i in entry_iteration_var_list:
  50. # download the html page of the List of entrys
  51. response = urllib.request.urlopen(entry_list_link1 + str(i) + entry_list_link2)
  52. web_content = response.read().decode("UTF-8")
  53. # save interim results to files
  54. f = open("spiders/pages/" + key + str(i) + "entryList.html", "w+")
  55. f.write(web_content)
  56. f.close
  57. def parse_entry_list_data2dictionary(self, list_of_fdbs):
  58. for fdb in list_of_fdbs:
  59. try:
  60. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  61. except Exception as e:
  62. print(
  63. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  64. e,
  65. )
  66. for i in iteration_var_list:
  67. print(i)
  68. try:
  69. # use soupparser to handle broken html
  70. tree = lxml.html.soupparser.parse(
  71. "spiders/pages/" + fdb + str(i) + "entryList.html"
  72. )
  73. print('oioioioioioioioioioioiOIOI')
  74. for e in tree.iter():
  75. print(e.tag)
  76. #
  77. for e in tree.xpath('//html//body//form//table//tr//td//table//tr'):
  78. print(etree.tostring(e).decode())
  79. dictionary_entry_list = {}
  80. fdb_conf = self.config.get(fdb)
  81. fdb_domain = fdb_conf.get("domain")
  82. fdb_conf_entry_list = fdb_conf.get("entry-list")
  83. fdb_conf_entry_list_parent = fdb_conf_entry_list.get("parent")
  84. fdb_conf_entry_list_child_name = fdb_conf_entry_list.get("child-name")
  85. fdb_conf_entry_list_child_link = fdb_conf_entry_list.get("child-link")
  86. for n in range(len(tree.xpath(fdb_conf_entry_list_parent))):
  87. name = tree.xpath(
  88. fdb_conf_entry_list_parent
  89. + "["
  90. + str(n)
  91. + "]"
  92. + fdb_conf_entry_list_child_name
  93. )
  94. link = tree.xpath(
  95. fdb_conf_entry_list_parent
  96. + "["
  97. + str(n)
  98. + "]"
  99. + fdb_conf_entry_list_child_link
  100. )
  101. print('oi' + name)
  102. if len(name) > 0:
  103. dictionary_entry_list[n] = {}
  104. dictionary_entry_list[n]["name"] = name[0]
  105. if fdb_domain in link[0]:
  106. dictionary_entry_list[n]["link"] = link[0]
  107. if fdb_domain not in link[0]:
  108. dictionary_entry_list[n]["link"] = fdb_domain + link[0]
  109. except Exception as e:
  110. print(
  111. "parsing the html did not work. Possibly you first have to run download_link_list_pages_of_funding_databases(). The original error message is:",
  112. e,
  113. )
  114. # save interim results to files
  115. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  116. f.write(str(dictionary_entry_list))
  117. f.close
  118. def download_entry_data_htmls(self, list_of_fdbs):
  119. for fdb in list_of_fdbs:
  120. try:
  121. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  122. except Exception as e:
  123. print(
  124. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  125. e,
  126. )
  127. for i in iteration_var_list:
  128. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  129. text = f.read()
  130. dictionary_entry_list = eval(text)
  131. for entry_id in dictionary_entry_list:
  132. entry_link = dictionary_entry_list[entry_id]["link"]
  133. # download the html page of the entry
  134. response = urllib.request.urlopen(entry_link)
  135. web_content = response.read().decode("UTF-8")
  136. # save interim results to files
  137. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  138. os.makedirs(os.path.dirname(file_name), exist_ok=True)
  139. f = open(file_name, "w+")
  140. f.write(web_content)
  141. f.close
  142. def parse_entry_data2dictionary(self, list_of_fdbs):
  143. for fdb in list_of_fdbs:
  144. try:
  145. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  146. except Exception as e:
  147. print(
  148. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  149. e,
  150. )
  151. for i in iteration_var_list:
  152. print("started to parse data of entry of " + fdb + " ..")
  153. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  154. text = f.read()
  155. dictionary_entry_list = eval(text)
  156. fdb_conf = self.config.get(fdb)
  157. fdb_domain = fdb_conf.get("domain")
  158. fdb_conf_entry = fdb_conf.get("entry")
  159. fdb_conf_entry_info1 = fdb_conf_entry.get("info-1")
  160. fdb_conf_entry_info1_parent = fdb_conf_entry_info1.get("parent")
  161. fdb_conf_entry_info1_child_1 = fdb_conf_entry_info1.get(
  162. "child-1"
  163. )
  164. for entry_id in dictionary_entry_list:
  165. print(
  166. "started to parse data of entry with name "
  167. + dictionary_entry_list[entry_id]["name"]
  168. + " .."
  169. )
  170. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  171. tree = lxml.html.soupparser.parse(file_name)
  172. child_1 = tree.xpath(
  173. fdb_conf_entry_info1_parent
  174. + fdb_conf_entry_info1_child_1
  175. )
  176. print("oi", child_1)
  177. if len(child_1) > 0:
  178. dictionary_entry_list[entry_id]["child_1"] = child_1[
  179. 0
  180. ]
  181. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  182. f.write(str(dictionary_entry_list))
  183. f.close