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.

784 lines
37 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. import requests
  10. from trafilatura import extract
  11. from pdfminer.high_level import extract_pages
  12. from pdfminer.layout import LTTextContainer
  13. class fdb_spider(object):
  14. def __init__(self, config_file):
  15. with open(config_file, "r") as stream:
  16. try:
  17. self.config = yaml.safe_load(stream)
  18. except yaml.YAMLError as exc:
  19. print(exc)
  20. # input list of funding databases in form of yaml file ['foerderinfo.bund.de', 'ausschreibungen.giz.de', .. , 'usw']
  21. def download_entry_list_pages_of_funding_databases(self, list_of_fdbs):
  22. # download only html pages of the funding databases specified in input
  23. for fdb in list_of_fdbs:
  24. for key in self.config:
  25. if key in list_of_fdbs:
  26. try:
  27. entry_list = self.config.get(key).get("entry-list")
  28. except Exception as e:
  29. print(
  30. "There is a problem with the configuration variable entryList in the config.yaml - the original error message is:",
  31. e,
  32. )
  33. try:
  34. entry_list_link1 = entry_list.get("link1")
  35. except Exception as e:
  36. print(
  37. "No link1 defined in config.yaml - the original error message is:",
  38. e,
  39. )
  40. try:
  41. entry_list_link2 = entry_list.get("link2")
  42. except Exception as e:
  43. print(
  44. "No link2 defined in config.yaml - the original error message is:",
  45. e,
  46. )
  47. try:
  48. entry_list_jslink1 = entry_list.get("jslink1")
  49. except Exception as e:
  50. print(
  51. "No jslink1 defined in config.yaml - the original error message is:",
  52. e,
  53. )
  54. entry_list_jslink1 = 'NONE'
  55. try:
  56. entry_list_jslink2 = entry_list.get("jslink2")
  57. except Exception as e:
  58. print(
  59. "No jslink2 defined in config.yaml - the original error message is:",
  60. e,
  61. )
  62. entry_list_jslink2 = 'NONE'
  63. try:
  64. entry_iteration_var_list = eval(entry_list.get("iteration-var-list"))
  65. except Exception as e:
  66. print(
  67. "No iteration-var-list defined in config.yaml - the original error message is:",
  68. e,
  69. )
  70. try:
  71. entry_jsdomain = eval(entry_list.get("jsdomain"))
  72. except Exception as e:
  73. print(
  74. "No iteration-var-list defined in config.yaml - the original error message is:",
  75. e,
  76. )
  77. entry_jsdomain = 'NONE'
  78. if entry_jsdomain == 'NONE':
  79. for i in entry_iteration_var_list:
  80. # download the html page of the List of entrys
  81. response = urllib.request.urlopen(entry_list_link1 + str(i) + entry_list_link2)
  82. # web_content = response.read().decode("UTF-8")
  83. try:
  84. web_content = response.read().decode("UTF-8")
  85. except Exception as e:
  86. try:
  87. web_content = response.read().decode("latin-1")
  88. print(
  89. "decoding the respone in utf8 did not work, try to decode latin1 now - the original error message is:",
  90. e,
  91. )
  92. except Exception as ex:
  93. print(ex)
  94. # save interim results to files
  95. if (len(web_content)) < 10:
  96. print('getting the html page through urllib did not work, trying with requests librarys function get')
  97. try:
  98. res = requests.get(entry_list_link1 + str(i) + entry_list_link2)
  99. web_content = res.text
  100. except Exception as e:
  101. print('also requests library did not work, original error is:', e)
  102. # print(web_content)
  103. f = open("spiders/pages/" + key + str(i) + "entryList.html", "w+")
  104. f.write(web_content)
  105. f.close
  106. else:
  107. from selenium import webdriver
  108. options = webdriver.ChromeOptions()
  109. options.add_argument('headless')
  110. driver = webdriver.Chrome(options=options)
  111. def find_config_parameter(self, list_of_fdbs):
  112. for fdb in list_of_fdbs:
  113. try:
  114. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  115. except Exception as e:
  116. print(
  117. "There is a problem with the configuration variable entryList iteration var list in the config.yaml",
  118. e,
  119. )
  120. fdb_conf = self.config.get(fdb)
  121. fdb_domain = fdb_conf.get("domain")
  122. fdb_conf_entry_list = fdb_conf.get("entry-list")
  123. fdb_conf_entry_list_parent = fdb_conf_entry_list.get("parent")
  124. fdb_conf_entry_list_child_name = fdb_conf_entry_list.get("child-name")
  125. fdb_conf_entry_list_child_link = fdb_conf_entry_list.get("child-link")
  126. fdb_conf_entry_list_child_info = fdb_conf_entry_list.get("child-info")
  127. fdb_conf_entry_list_child_period = fdb_conf_entry_list.get("child-period")
  128. for i in iteration_var_list:
  129. print(i)
  130. try:
  131. # use soupparser to handle broken html
  132. tree = lxml.html.soupparser.parse(
  133. "spiders/pages/" + fdb + str(i) + "entryList.html"
  134. )
  135. except Exception as e:
  136. tree = html.parse("spiders/pages/" + fdb + str(i) + "entryList.html")
  137. print(
  138. "parsing the xml files did not work with the soupparser. Broken html will not be fixed as it could have been",
  139. e,
  140. )
  141. try:
  142. print('this is the n looped elements of the parent specified in config.yaml:')
  143. #print('entrylistparent', fdb_conf_entry_list_parent)
  144. #print(tree.xpath("//html//body//div//main//div//div[@class='row']//section[@class='l-search-result-list']"))
  145. #print(etree.tostring(tree.xpath(fdb_conf_entry_list_parent)).decode())
  146. for n in range(len(tree.xpath(fdb_conf_entry_list_parent))):
  147. print('-----------------------------------------------------------------------------------------------------------------------------------------')
  148. print(etree.tostring(tree.xpath(fdb_conf_entry_list_parent)[n]).decode())
  149. print('this is the name children:')
  150. name_element = tree.xpath(fdb_conf_entry_list_parent + fdb_conf_entry_list_child_name)
  151. print(name_element)
  152. #for name in name_element:
  153. # print(name)
  154. print(len(name_element))
  155. print('this is the link children:')
  156. link_element = tree.xpath(fdb_conf_entry_list_parent + fdb_conf_entry_list_child_link)
  157. print(link_element)
  158. #for link in link_element:
  159. # print(link)
  160. print(len(link_element))
  161. print('this is the info children:')
  162. info_element = tree.xpath(fdb_conf_entry_list_parent + fdb_conf_entry_list_child_info)
  163. print(info_element)
  164. print(len(info_element))
  165. print('this is the period children:')
  166. period_element = tree.xpath(fdb_conf_entry_list_parent + fdb_conf_entry_list_child_period)
  167. print(period_element)
  168. print(len(period_element))
  169. except Exception as e:
  170. print(
  171. "parsing the html did not work.",
  172. e,
  173. )
  174. def parse_entry_list_data2dictionary(self, list_of_fdbs):
  175. for fdb in list_of_fdbs:
  176. try:
  177. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  178. except Exception as e:
  179. print(
  180. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  181. e,
  182. )
  183. for i in iteration_var_list:
  184. print(i)
  185. try:
  186. # use soupparser to handle broken html
  187. tree = lxml.html.soupparser.parse(
  188. "spiders/pages/" + fdb + str(i) + "entryList.html"
  189. )
  190. except Exception as e:
  191. tree = html.parse("spiders/pages/" + fdb + str(i) + "entryList.html")
  192. print(
  193. "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:",
  194. e,
  195. )
  196. try:
  197. #print('this is the n looped elements of the parent specified in config.yaml:')
  198. #for e in tree.iter():
  199. # print(e.tag)
  200. #
  201. #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()]"):
  202. #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']"):
  203. # print(etree.tostring(e).decode())
  204. dictionary_entry_list = {}
  205. fdb_conf = self.config.get(fdb)
  206. fdb_domain = fdb_conf.get("domain")
  207. fdb_conf_entry_list = fdb_conf.get("entry-list")
  208. fdb_conf_entry_list_parent = fdb_conf_entry_list.get("parent")
  209. fdb_conf_entry_list_child_name = fdb_conf_entry_list.get("child-name")
  210. fdb_conf_entry_list_child_link = fdb_conf_entry_list.get("child-link")
  211. fdb_conf_entry_list_child_info = fdb_conf_entry_list.get("child-info")
  212. fdb_conf_entry_list_child_period = fdb_conf_entry_list.get("child-period")
  213. #print('blabliblub')
  214. #print('len', len(tree.xpath(fdb_conf_entry_list_parent)))
  215. for n in range(len(tree.xpath(fdb_conf_entry_list_parent))):
  216. try:
  217. name = tree.xpath(
  218. fdb_conf_entry_list_parent
  219. + "["
  220. + str(n+1)
  221. + "]"
  222. + fdb_conf_entry_list_child_name
  223. )[0]
  224. except Exception as e:
  225. print("name could not be parsed", e)
  226. name = 'NONE'
  227. try:
  228. info = tree.xpath(
  229. fdb_conf_entry_list_parent
  230. + "["
  231. + str(n+1)
  232. + "]"
  233. + fdb_conf_entry_list_child_info
  234. )[0]
  235. except Exception as e:
  236. print("info could not be parsed", e, info)
  237. info = 'NONE'
  238. try:
  239. period = tree.xpath(
  240. fdb_conf_entry_list_parent
  241. + "["
  242. + str(n+1)
  243. + "]"
  244. + fdb_conf_entry_list_child_period
  245. )[0]
  246. #print('period', period)
  247. except Exception as e:
  248. print("period could not be parsed", e, period)
  249. period = 'NONE'
  250. try:
  251. link = tree.xpath(
  252. fdb_conf_entry_list_parent
  253. + "["
  254. + str(n+1)
  255. + "]"
  256. + fdb_conf_entry_list_child_link
  257. )[0]
  258. if 'javascript:' in link:
  259. #from selenium import webdriver
  260. print('link is javascript element, not url to parse')
  261. #url = 'https://example.com'
  262. #driver = webdriver.Chrome()
  263. #driver.get(url)
  264. #links = [link.get_attribute('href') for link in driver.find_elements_by_tag_name('a')]
  265. #print('link', link)
  266. except Exception as e:
  267. print("link could not be parsed", e, link)
  268. link = 'NONE'
  269. if len(name) > 0 and name != 'NONE':
  270. dictionary_entry_list[n] = {}
  271. dictionary_entry_list[n]["name"] = name
  272. dictionary_entry_list[n]["info"] = info
  273. dictionary_entry_list[n]["period"] = period
  274. if fdb_domain in link:
  275. dictionary_entry_list[n]["link"] = link
  276. if fdb_domain not in link and ('http:' in link or 'www.' in link or 'https:' in link):
  277. dictionary_entry_list[n]["link"] = link
  278. if 'javascript:' in link:
  279. dictionary_entry_list[n]["link"] = link
  280. else:
  281. if link[-1] == '/':
  282. dictionary_entry_list[n]["link"] = fdb_domain + link
  283. else:
  284. dictionary_entry_list[n]["link"] = fdb_domain + '/' + link
  285. except Exception as e:
  286. print(
  287. "parsing the html did not work. Possibly you first have to run download_link_list_pages_of_funding_databases(). The original error message is:",
  288. e,
  289. )
  290. # save interim results to files
  291. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  292. f.write(str(dictionary_entry_list))
  293. f.close
  294. def download_entry_data_htmls(self, list_of_fdbs):
  295. from selenium import webdriver
  296. options = webdriver.ChromeOptions()
  297. options.add_argument('headless')
  298. driver = webdriver.Chrome(options=options)
  299. for fdb in list_of_fdbs:
  300. try:
  301. iteration_var_list = eval(self.config.get(fdb).get("entry-list").get("iteration-var-list"))
  302. except Exception as e:
  303. print(
  304. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  305. e,
  306. )
  307. print('starting to download the entry html pages..')
  308. for i in iteration_var_list:
  309. print(i)
  310. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  311. text = f.read()
  312. dictionary_entry_list = eval(text)
  313. fdb_conf = self.config.get(fdb)
  314. fdb_domain = fdb_conf.get("domain")
  315. fdb_conf_entry_list = fdb_conf.get("entry-list")
  316. fdb_conf_entry_list_parent = fdb_conf_entry_list.get("parent")
  317. fdb_conf_entry_list_child_name = fdb_conf_entry_list.get("child-name")
  318. try:
  319. fdb_conf_entry_list_javascript_link = fdb_conf_entry_list.get("javascript-link")
  320. except Exception as e:
  321. print('the javascript link in the config is missing, original error message is:', e)
  322. fdb_conf_entry_list_link1 = fdb_conf_entry_list.get("link1")
  323. fdb_conf_entry_list_link2 = fdb_conf_entry_list.get("link2")
  324. driver.get(fdb_conf_entry_list_link1 + str(i) + fdb_conf_entry_list_link2)
  325. for entry_id in dictionary_entry_list:
  326. entry_link = dictionary_entry_list[entry_id]["link"]
  327. web_content = 'NONE'
  328. # download the html page of the entry
  329. if 'javascript' in entry_link:
  330. element = driver.find_element(
  331. "xpath",
  332. fdb_conf_entry_list_parent
  333. + "["
  334. + str(entry_id+1)
  335. + "]"
  336. + fdb_conf_entry_list_javascript_link
  337. )
  338. # to time.sleep was suggested for errors
  339. #import time
  340. #time.sleep(1)
  341. element.click()
  342. window_after = driver.window_handles[1]
  343. driver.switch_to.window(window_after)
  344. #element = driver.find_element("xpath", "//html")
  345. #web_content = element.text
  346. #entry_domain = driver.getCurrentUrl()
  347. entry_domain = driver.current_url
  348. dictionary_entry_list[entry_id]["domain"] = entry_domain
  349. web_content = driver.page_source
  350. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  351. os.makedirs(os.path.dirname(file_name), exist_ok=True)
  352. f = open(file_name, "w+")
  353. f.write(web_content)
  354. f.close
  355. window_before = driver.window_handles[0]
  356. driver.switch_to.window(window_before)
  357. if ('http' or 'www') in entry_link and ('javascript' or 'js' or '.pdf') not in enry_link:
  358. try:
  359. # defining cookie to not end up in endless loop because of cookie banners pointing to redirects
  360. url = entry_link
  361. req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0', 'Cookie':'myCookie=lovely'})
  362. response = urllib.request.urlopen(req)
  363. except Exception as e:
  364. try:
  365. response = urllib.request.urlopen(entry_link.encode('ascii', errors='xmlcharrefreplace').decode('ascii'))
  366. print(
  367. "opening the link did not work, try to encode to ascii replacing xmlcharrefs now and reopen - the original error message is:",
  368. e,
  369. )
  370. except Exception as ex:
  371. print(entry_link, entry_link.encode('ascii', errors='xmlcharrefreplace').decode('ascii'), ex )
  372. try:
  373. web_content = response.read().decode("UTF-8")
  374. except Exception as e:
  375. try:
  376. web_content = response.read().decode("latin-1")
  377. print(
  378. "decoding the respone in utf8 did not work, try to decode latin1 now - the original error message is:",
  379. e,
  380. )
  381. except Exception as ex:
  382. print(ex)
  383. # save interim results to files
  384. if '.pdf' in entry_link:
  385. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  386. response = requests.get(entry_link)
  387. os.makedirs(os.path.dirname(file_name), exist_ok=True)
  388. f = open(file_name, "bw")
  389. f.write(response.content)
  390. f.close
  391. else:
  392. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  393. if web_content == 'NONE':
  394. print('other downloading approaches did not work, trying requests')
  395. try:
  396. from requests_html import HTMLSession
  397. session = HTMLSession()
  398. r = session.get(entry_link)
  399. r.html.render()
  400. web_content = r.text
  401. except Exception as e:
  402. print('requests_html HTMLSession did not work')
  403. os.makedirs(os.path.dirname(file_name), exist_ok=True)
  404. f = open(file_name, "w+")
  405. f.write(web_content)
  406. f.close
  407. # save the entry_domain, implemented first for further downloads in javascript links
  408. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  409. f.write(str(dictionary_entry_list))
  410. f.close
  411. def parse_entry_data2dictionary(self, list_of_fdbs):
  412. for fdb in list_of_fdbs:
  413. try:
  414. fdb_config = self.config.get(fdb)
  415. print('oi oi',fdb_config)
  416. fdb_config_entrylist = fdb_config.get("entry-list")
  417. iteration_var_list = eval(fdb_config_entrylist.get("iteration-var-list"))
  418. except Exception as e:
  419. print(
  420. "There is a problem with the configuration variable entryList iteration var list in the config.yaml - the original error message is:",
  421. e,
  422. )
  423. for i in iteration_var_list:
  424. print("started to parse data of entry of " + fdb + " ..")
  425. f = open("spiders/output/" + fdb + str(i) + "entryList.txt")
  426. text = f.read()
  427. dictionary_entry_list = eval(text)
  428. fdb_conf = self.config.get(fdb)
  429. fdb_domain = fdb_conf.get("domain")
  430. fdb_conf_entry = fdb_conf.get("entry")
  431. #print('balubaluba', fdb_conf_entry)
  432. fdb_conf_entry_general = fdb_conf_entry.get("general")
  433. #print(fdb_conf_entry_general)
  434. for entry_id in dictionary_entry_list:
  435. print(
  436. "started to parse data of entry with name "
  437. + dictionary_entry_list[entry_id]["name"]
  438. + " .."
  439. )
  440. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  441. try:
  442. tree = lxml.html.soupparser.parse(file_name)
  443. except Exception as e:
  444. tree = html.parse(file_name)
  445. print(
  446. "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:",
  447. e,
  448. )
  449. if fdb_conf_entry_general["uniform"] == 'TRUE':
  450. fdb_conf_entry_unitrue = fdb_conf_entry.get("unitrue")
  451. for key in fdb_conf_entry_unitrue:
  452. fdb_conf_entry_unitrue_child = fdb_conf_entry_unitrue.get(key)
  453. child = tree.xpath(
  454. fdb_conf_entry_unitrue_child
  455. )[0]
  456. print("oi", child)
  457. if '.pdf' in child:
  458. print('child in entry data is pdf, downloading it..')
  459. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".pdf"
  460. entry_link = dictionary_entry_list[entry_id]["link"]
  461. if 'http' not in child:
  462. if 'javascript' or 'js' not in entry_link and 'http' in entry_link:
  463. try:
  464. response = requests.get(entry_link + child)
  465. except Exception as e:
  466. print(entry_link + child + ' seems not a valid pdf link to download, orginal error message is:', e)
  467. if 'javascript' or 'js' in entry_link:
  468. entry_domain = dictionary_entry_list[entry_id]["domain"]
  469. if child[0] == '.' and child[1] == '/':
  470. if entry_domain[-1] == '/':
  471. pdf_link = entry_domain[:-1] + child[1:]
  472. if entry_domain[-1] != '/':
  473. for n in range(len(entry_domain)):
  474. if entry_domain[-1] != '/':
  475. entry_domain = entry_domain[:-1]
  476. else:
  477. break
  478. pdf_link = entry_domain + child[1:]
  479. if child[0] == '/':
  480. if entry_domain[-1] == '/':
  481. pdf_link = entry_domain[:-1] + child
  482. if entry_domain[-1] != '/':
  483. pdf_link = entry_domain + child
  484. print('pdf_link', pdf_link)
  485. try:
  486. response = requests.get(pdf_link)
  487. except Exception as e:
  488. print(pdf_link + ' seems not a valid pdf link to download, orginal error message is:', e)
  489. #response = requests.get(child)
  490. os.makedirs(os.path.dirname(file_name), exist_ok=True)
  491. f = open(file_name, "bw")
  492. f.write(response.content)
  493. f.close
  494. print('parsing a pdf', pdf_link, entry_id)
  495. try:
  496. generaltext = ''
  497. for page_layout in extract_pages(file_name):
  498. for element in page_layout:
  499. if isinstance(element, LTTextContainer):
  500. generaltext += element.get_text()
  501. except Exception as e:
  502. generaltext = 'NONE'
  503. print('parsing pdf did not work, the original error is:', e )
  504. dictionary_entry_list[entry_id][key] = generaltext
  505. if len(child) > 0 and '.pdf' not in child:
  506. dictionary_entry_list[entry_id][key] = child[
  507. 0
  508. ]
  509. else:
  510. fdb_conf_entry_unifalse = fdb_conf_entry.get("unifalse")
  511. fdb_conf_entry_unifalse_wordlist = fdb_conf_entry_unifalse.get("wordlist")
  512. if '.pdf' in dictionary_entry_list[entry_id]["link"]:
  513. print('parsing a pdf', dictionary_entry_list[entry_id]["link"], entry_id)
  514. try:
  515. generaltext = ''
  516. for page_layout in extract_pages(file_name):
  517. for element in page_layout:
  518. if isinstance(element, LTTextContainer):
  519. generaltext += element.get_text()
  520. except Exception as e:
  521. generaltext = 'NONE'
  522. print('parsing pdf did not work, the original error is:', e )
  523. else:
  524. p_text = tree.xpath(
  525. "//p//text()"
  526. )
  527. div_text = tree.xpath(
  528. "//div//text()"
  529. )
  530. #print("oi", text)
  531. generaltext = ''
  532. for n in range(len(p_text)):
  533. if len(p_text[n]) > 0:
  534. generaltext += p_text[n] + ' '
  535. for n in range(len(div_text)):
  536. if len(div_text[n]) > 0 and div_text[n] not in p_text:
  537. generaltext += div_text[n] + ' '
  538. generaltextlist = generaltext.split(' ')
  539. if len(generaltextlist) > 5000:
  540. print('text over 1000 words for entry id', entry_id, ' number of words:', len(generaltextlist))
  541. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  542. try:
  543. with open(file_name , 'r', encoding='utf-8') as file:
  544. html_content = file.read()
  545. except Exception as e:
  546. with open(file_name , 'r', encoding='latin-1') as file:
  547. html_content = file.read()
  548. print('encoding utf8 in opening with trafilatura did not work, trying latin1, original error message is:', e)
  549. generaltext = extract(html_content)
  550. print('generaltext word count was: ', len(generaltextlist), 'but now trafilatura did the job and new wordcount is:', len(generaltext.split(' ')))
  551. if len(generaltextlist) < 2:
  552. print('no text parsed, the wc is', len(generaltextlist))
  553. print('text under 2 words for entry id', entry_id, ' number of words:', len(generaltextlist))
  554. file_name = "spiders/pages/" + fdb + str(i) + "/" + str(entry_id) + ".html"
  555. try:
  556. with open(file_name , 'r', encoding='utf-8') as file:
  557. html_content = file.read()
  558. except Exception as e:
  559. with open(file_name , 'r', encoding='latin-1') as file:
  560. html_content = file.read()
  561. print('encoding utf8 in opening with trafilatura did not work, trying latin1, original error message is:', e)
  562. generaltext = extract(html_content)
  563. try:
  564. if len(generaltext) > 2:
  565. print('generaltext word count was: ', len(generaltextlist), 'but now trafilatura did the job and new wordcount is:', len(generaltext.split(' ')))
  566. except:
  567. print('trafilatura got this out:', generaltext , 'setting generaltext to NONE')
  568. generaltext = 'NONE'
  569. dictionary_entry_list[entry_id]["text"] = generaltext
  570. dictionary_entry_list[entry_id]["text-word-count"] = len(generaltextlist)
  571. f = open("spiders/output/" + fdb + str(i) + "entryList.txt", "w+")
  572. f.write(str(dictionary_entry_list))
  573. f.close