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.

332 lines
14 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 find_config_parameter(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",
  65. e,
  66. )
  67. fdb_conf = self.config.get(fdb)
  68. fdb_domain = fdb_conf.get("domain")
  69. fdb_conf_entry_list = fdb_conf.get("entry-list")
  70. fdb_conf_entry_list_parent = fdb_conf_entry_list.get("parent")
  71. fdb_conf_entry_list_child_name = fdb_conf_entry_list.get("child-name")
  72. fdb_conf_entry_list_child_link = fdb_conf_entry_list.get("child-link")
  73. for i in iteration_var_list:
  74. print(i)
  75. try:
  76. # use soupparser to handle broken html
  77. tree = lxml.html.soupparser.parse(
  78. "spiders/pages/" + fdb + str(i) + "entryList.html"
  79. )
  80. except Exception as e:
  81. tree = html.parse("spiders/pages/" + fdb + str(i) + "entryList.html")
  82. print(
  83. "parsing the xml files did not work with the soupparser. Broken html will not be fixed as it could have been",
  84. e,
  85. )
  86. try:
  87. print('this is the n looped elements of the parent specified in config.yaml:')
  88. #print('entrylistparent', fdb_conf_entry_list_parent)
  89. #print(tree.xpath("//html//body//div//main//div//div[@class='row']//section[@class='l-search-result-list']"))
  90. #print(etree.tostring(tree.xpath(fdb_conf_entry_list_parent)).decode())
  91. for n in range(len(tree.xpath(fdb_conf_entry_list_parent))):
  92. print('-----------------------------------------------------------------------------------------------------------------------------------------')
  93. print(etree.tostring(tree.xpath(fdb_conf_entry_list_parent)[n]).decode())
  94. print('this is the first actual name element:')
  95. name_element = tree.xpath(fdb_conf_entry_list_parent + fdb_conf_entry_list_child_name)
  96. print(name_element)
  97. for name in name_element:
  98. print(name)
  99. print('this is the first actual link element:')
  100. link_element = tree.xpath(fdb_conf_entry_list_parent + fdb_conf_entry_list_child_link)
  101. print(link_element)
  102. #for link in link_element:
  103. # print(link)
  104. except Exception as e:
  105. print(
  106. "parsing the html did not work.",
  107. e,
  108. )
  109. def parse_entry_list_data2dictionary(self, list_of_fdbs):
  110. for fdb in list_of_fdbs:
  111. try:
  112. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  113. except Exception as e:
  114. print(
  115. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  116. e,
  117. )
  118. for i in iteration_var_list:
  119. print(i)
  120. try:
  121. # use soupparser to handle broken html
  122. tree = lxml.html.soupparser.parse(
  123. "spiders/pages/" + fdb + str(i) + "entryList.html"
  124. )
  125. except Exception as e:
  126. tree = html.parse("spiders/pages/" + fdb + str(i) + "entryList.html")
  127. print(
  128. "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:",
  129. e,
  130. )
  131. try:
  132. #print('this is the n looped elements of the parent specified in config.yaml:')
  133. #for e in tree.iter():
  134. # print(e.tag)
  135. #
  136. #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()]"):
  137. #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']"):
  138. # print(etree.tostring(e).decode())
  139. dictionary_entry_list = {}
  140. fdb_conf = self.config.get(fdb)
  141. fdb_domain = fdb_conf.get("domain")
  142. fdb_conf_entry_list = fdb_conf.get("entry-list")
  143. fdb_conf_entry_list_parent = fdb_conf_entry_list.get("parent")
  144. fdb_conf_entry_list_child_name = fdb_conf_entry_list.get("child-name")
  145. fdb_conf_entry_list_child_link = fdb_conf_entry_list.get("child-link")
  146. print('blabliblub')
  147. print('len', len(tree.xpath(fdb_conf_entry_list_parent)))
  148. for n in range(len(tree.xpath(fdb_conf_entry_list_parent))):
  149. print('oi inside the loop')
  150. name = tree.xpath(
  151. fdb_conf_entry_list_parent
  152. + fdb_conf_entry_list_child_name
  153. )[n]
  154. print('oi ', name)
  155. print('blablidubbiduub')
  156. link = tree.xpath(
  157. fdb_conf_entry_list_parent
  158. # + "["
  159. # + str(n)
  160. # + "]"
  161. + fdb_conf_entry_list_child_link
  162. )[n]
  163. print('oi' + name)
  164. if len(name) > 0:
  165. dictionary_entry_list[n] = {}
  166. dictionary_entry_list[n]["name"] = name
  167. if fdb_domain in link:
  168. dictionary_entry_list[n]["link"] = link
  169. if fdb_domain not in link:
  170. if link[-1] == '/':
  171. dictionary_entry_list[n]["link"] = fdb_domain + link
  172. else:
  173. dictionary_entry_list[n]["link"] = fdb_domain + '/' + link
  174. except Exception as e:
  175. print(
  176. "parsing the html did not work. Possibly you first have to run download_link_list_pages_of_funding_databases(). The original error message is:",
  177. e,
  178. )
  179. # save interim results to files
  180. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  181. f.write(str(dictionary_entry_list))
  182. f.close
  183. def download_entry_data_htmls(self, list_of_fdbs):
  184. for fdb in list_of_fdbs:
  185. try:
  186. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  187. except Exception as e:
  188. print(
  189. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  190. e,
  191. )
  192. for i in iteration_var_list:
  193. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  194. text = f.read()
  195. dictionary_entry_list = eval(text)
  196. for entry_id in dictionary_entry_list:
  197. entry_link = dictionary_entry_list[entry_id]["link"]
  198. # download the html page of the entry
  199. response = urllib.request.urlopen(entry_link)
  200. web_content = response.read().decode("UTF-8")
  201. # save interim results to files
  202. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  203. os.makedirs(os.path.dirname(file_name), exist_ok=True)
  204. f = open(file_name, "w+")
  205. f.write(web_content)
  206. f.close
  207. def parse_entry_data2dictionary(self, list_of_fdbs):
  208. for fdb in list_of_fdbs:
  209. try:
  210. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  211. except Exception as e:
  212. print(
  213. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  214. e,
  215. )
  216. for i in iteration_var_list:
  217. print("started to parse data of entry of " + fdb + " ..")
  218. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  219. text = f.read()
  220. dictionary_entry_list = eval(text)
  221. fdb_conf = self.config.get(fdb)
  222. fdb_domain = fdb_conf.get("domain")
  223. fdb_conf_entry = fdb_conf.get("entry")
  224. fdb_conf_entry_info1 = fdb_conf_entry.get("info-1")
  225. fdb_conf_entry_info1_parent = fdb_conf_entry_info1.get("parent")
  226. fdb_conf_entry_info1_child_1 = fdb_conf_entry_info1.get(
  227. "child-1"
  228. )
  229. for entry_id in dictionary_entry_list:
  230. print(
  231. "started to parse data of entry with name "
  232. + dictionary_entry_list[entry_id]["name"]
  233. + " .."
  234. )
  235. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  236. tree = lxml.html.soupparser.parse(file_name)
  237. child_1 = tree.xpath(
  238. fdb_conf_entry_info1_parent
  239. + fdb_conf_entry_info1_child_1
  240. )
  241. print("oi", child_1)
  242. if len(child_1) > 0:
  243. dictionary_entry_list[entry_id]["child_1"] = child_1[
  244. 0
  245. ]
  246. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  247. f.write(str(dictionary_entry_list))
  248. f.close