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.

237 lines
9.3 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 + string(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(webContent)
  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. try:
  68. # use soupparser to handle broken html
  69. tree = lxml.html.soupparser.parse(
  70. "spiders/pages/" + fdb + str(i) + "entryList.html"
  71. )
  72. # for e in tree.iter():
  73. #
  74. # print(e.tag)
  75. #
  76. # for e in tree.xpath('//html//body//form//table//tr//td//table//tr'):
  77. #
  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. if len(name) > 0:
  102. dictionary_entry_list[n] = {}
  103. dictionary_entry_list[n]["name"] = name[0]
  104. if fdb_domain in link[0]:
  105. dictionary_entry_list[n]["link"] = link[0]
  106. if fdb_domain not in link[0]:
  107. dictionary_entry_list[n]["link"] = fdb_domain + link[0]
  108. except Exception as e:
  109. print(
  110. "parsing the html did not work. Possibly you first have to run download_link_list_pages_of_funding_databases(). The original error message is:",
  111. e,
  112. )
  113. # save interim results to files
  114. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  115. f.write(str(dictionary_entry_list))
  116. f.close
  117. def download_entry_data_htmls(self, list_of_fdbs):
  118. for fdb in list_of_fdbs:
  119. try:
  120. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  121. except Exception as e:
  122. print(
  123. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  124. e,
  125. )
  126. for i in iteration_var_list:
  127. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  128. text = f.read()
  129. dictionary_entry_list = eval(text)
  130. for entry_id in dictionary_entry_list:
  131. entry_link = dictionary_entry_list[entry_id]["link"]
  132. # download the html page of the entry
  133. response = urllib.request.urlopen(entry_link)
  134. web_content = response.read().decode("UTF-8")
  135. # save interim results to files
  136. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  137. os.makedirs(os.path.dirname(file_name), exist_ok=True)
  138. f = open(file_name, "w+")
  139. f.write(web_content)
  140. f.close
  141. def parse_entry_data2dictionary(self, list_of_fdbs):
  142. for fdb in list_of_fdbs:
  143. try:
  144. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  145. except Exception as e:
  146. print(
  147. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  148. e,
  149. )
  150. for i in iteration_var_list:
  151. print("started to parse data of entry of " + fdb + " ..")
  152. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  153. text = f.read()
  154. dictionary_entry_list = eval(text)
  155. fdb_conf = self.config.get(fdb)
  156. fdb_domain = fdb_conf.get("domain")
  157. fdb_conf_entry = fdb_conf.get("entry")
  158. fdb_conf_entry_info1 = fdb_conf_entry.get("info-1")
  159. fdb_conf_entry_info1_parent = fdb_conf_entry_info1.get("parent")
  160. fdb_conf_entry_info1_child_1 = fdb_conf_entry_info1.get(
  161. "child-1"
  162. )
  163. for entry_id in dictionary_entry_list:
  164. print(
  165. "started to parse data of entry with name "
  166. + dictionary_entry_list[entry_id]["name"]
  167. + " .."
  168. )
  169. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  170. tree = lxml.html.soupparser.parse(file_name)
  171. child_1 = tree.xpath(
  172. fdb_conf_entry_info1_parent
  173. + fdb_conf_entry_info1_child_1
  174. )
  175. print("oi", child_1)
  176. if len(child_1) > 0:
  177. dictionary_entry_list[entry_id]["child_1"] = child_1[
  178. 0
  179. ]
  180. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  181. f.write(str(dictionary_entry_list))
  182. f.close