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.

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