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.

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