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.

228 lines
9.6 KiB

11 months ago
11 months ago
11 months ago
  1. ```
  2. __ _ _ _ _
  3. / _| __| | |__ ___ _ __ (_) __| | ___ _ __
  4. | |_ / _` | '_ \ _____/ __| '_ \| |/ _` |/ _ | '__|
  5. | _| (_| | |_) |_____\__ | |_) | | (_| | __| |
  6. |_| \__,_|_.__/ |___| .__/|_|\__,_|\___|_|
  7. |_|
  8. ```
  9. 1. [Introduction](#introduction)
  10. 2. [Installation](#installation)
  11. 3. [Usage](#usage)
  12. * [Configuration File Syntax](#configuration-file-syntax)
  13. * [Efficient Xpath Copying](#efficient-xpath-copying)
  14. * [Step By Step Guide](#step-by-step-guide)
  15. - [var domain](#var-domain)
  16. - [var entry list](#var-entry-list)
  17. - [var link and iteration](#var-link-and-iteration)
  18. - [example1 link](#example1-link)
  19. - [example2 link](#example2-link)
  20. - [var jslink and jsiteration](#var-jslink-and-jsiteration)
  21. - [var parent](#var-parent)
  22. - [example1 parent](#example1-parent)
  23. - [var slow downloading](#var-slow-downloading)
  24. # Introduction
  25. The fdb-spider was made to gather data from Websites in an automated way.
  26. The Website to be spidered has to be a list of Links.
  27. Which makes the fdb-spider a web spider for most Plattforms.
  28. The fdb-spider is to be configured in a .yaml file to make things easy.
  29. The output of the fdb-spider is in json format to make it easy to input
  30. the json to other programs.
  31. At its core, the spider outputs tag search based entries
  32. It works together with the fdb-spider-interface.
  33. In Future, the spider will be extended by the model Sauerkraut.
  34. An !open source! Artificial Neural Network.
  35. # Installation
  36. Create a python3 virtualenv on your favourite UNIX Distribution
  37. with the command
  38. ```
  39. git clone https://code.basabuuka.org/alpcentaur/fdb-spider
  40. cd fdb-spider
  41. virtualenv venv
  42. source venv/bin/activate
  43. pip install -r requirements.txt
  44. ```
  45. then install systemwide requirements with your package manager
  46. ```
  47. # apt based unixoids
  48. apt install xvfb
  49. apt install chromium
  50. apt install chromium-webdriver
  51. # pacman based unixoids
  52. pacman -S xorg-server-xvfb
  53. pacman -S chromium
  54. ```
  55. # Usage
  56. ## Configuration File Syntax
  57. The configuration file with working syntax template is
  58. ```
  59. /spiders/config.yaml
  60. ```
  61. Here you can configure new websites to spider, referred to as "databases".
  62. link1 and link2 are the links to be iterated.
  63. The assumption is, that every list of links will have a loopable structure.
  64. If links are javascript links, specify js[domain,link[1,2],iteration-var-list].
  65. Otherwise leave them out, but specify jsdomain as 'None'.
  66. You will find parents and children of the entry list pages.
  67. Here you have to fill in the xpath of the entries to be parsed.
  68. In the entry directive, you have to specify uniform to either TRUE or FALSE.
  69. Set it to TRUE, if all the entry pages have the same template, and you
  70. are able to specify xpath again to get the text or whatever variable you
  71. like to specify.
  72. In the entry_unitrue directive, you can specify new dimensions and
  73. the json will adapt to your wishes.
  74. Under the entry-list directive this feature has to be still implemented.
  75. So use name, link, javascript-link, info, period and sponsor by commenting
  76. in or out.
  77. If javascript-link is set (which means its javascript clickable),
  78. link will be ignored.
  79. Set it to FALSE, if you have diverse pages behind the entries,
  80. and want to generally get the main text of all the different links.
  81. For your information, the library trafilature is used to gather the
  82. text generally for further processing.
  83. ## Efficient Xpath Copying
  84. When copying the Xpath, most modern Webbrowsers are of help.
  85. In Firefox (or Browsers build on it like the TOR Browser) you can use
  86. ```
  87. strl-shift-c
  88. ```
  89. to open the "Inspector" in "Pick an element" mode.
  90. When you click on the desired entry on the page,
  91. it opens the actual code of the clicked element in the html search box.
  92. Now make a right click on the code in the html search box, go on "Copy",
  93. and go on XPath.
  94. Now you have the XPath of the element in your clipboard.
  95. When pasting it into the config, try to replace some slashes with double
  96. slashes. That will make the spider more stable, in case the websites
  97. html/xml gets changed for maintenance or other reasons.
  98. ## Step By Step Guide
  99. Start with an old Configuration that is similar to what you need.
  100. There are Three Types of Configurations:
  101. The first Type is purely path based. An example is greenjobs.de.
  102. The second Type is a mixture of path and javascript functions, giz is an example for this Type.
  103. The third Type is purely javascript based. An example is ted.europe.eu.
  104. Type 1:
  105. Start with collecting every variable.
  106. From up to down.
  107. ### var domain
  108. domain is the variable for the root of the website.
  109. In case links are glued, they will be glued based on the root.
  110. ### var entry-list
  111. Now come all the variables regarding the entry list pages.
  112. #### var link1, link2 and iteration-var-list
  113. In Pseudo Code, whats happening with these three variables is
  114. ```
  115. for n in iteration var list:
  116. get(link1 + n + link2)
  117. ```
  118. So if you are on the no javascript side of reality, you are lucky. Thats all needed to get the collection of links.
  119. #### example 1 var link1
  120. Lets say we go on greenjobs.de.
  121. We go on search without search query. To get the biggest displayed output, in best case a table of everything the site has listed.
  122. https://www.greenjobs.de/angebote/index.html?s=&loc=&countrycode=de&dist=10&lng=&lat=
  123. is the resulting url.
  124. So now we navigate through the pages.
  125. In this case everything is displayed and scrollable on exactly this url. Which means, we leave link2 and iteration var list empty. And put the resulting url into link1.
  126. #### example 2 var link1
  127. This time we go on giz. There we have https://ausschreibungen.giz.de/Satellite/company/welcome.do as our url for a general search. If I go on the "nextpage" button of the displayed table, a new url pattern appears being on the next page:
  128. https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT=2
  129. Going on the next side again, we get the url:
  130. https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT=3
  131. So now we already see the pattern, that any and every machine generated output cant hide.
  132. RSULT=1 .... we put it in the url bar of the browser
  133. https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT=1
  134. and get to the first pages.
  135. Which leads to the following variables, considering that there were 6 pages:
  136. * link1 = "https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT="
  137. * link2 = ""
  138. * iteration-var-list = "[1,2,3,4,5,6]"
  139. #### var jslink1, jslink2, jsvar-iteration-list
  140. It happens, that it is not possible to see a pattern in the urls. Probably because the website hoster is not smart or just a thief in a bad sense. In this case you will be forced to use the selenium part of the spider.
  141. Which means we emulate a whole virtual "user" using a virtual "browser" on his or her virtual "screen". In the end the clickable elements are defined by xpath too ( with some double slashes).
  142. You can run the spider with display=1 instead of display=0 in the python line of the virtual display the chromium driver is running on. I will put that in the initialization of the spider.
  143. With running the spider while watching the automated mouse moves and clicks, you will be able to find the right xpath for every step and element.
  144. #### var parent
  145. The parent stands for the last xml element which contains the entry links. Go with the Inspector on the entry respectively one of the links, klick on it and klick in the code view afterwards. Now use the arrow up key to get to the last child before it comes to the parent. You can see it on the rendered html blue contained.
  146. From the last child, when it is selected in the code view of the Inspector, do a left click on it.
  147. Select copy --> full xpath.
  148. Now copy paste the xpath to the parent variable, and put a double slash in front of the last element in the route.
  149. #### example 1 var parent
  150. For the List of the Gesellschaft für internationale Zusammenarbeit the last child before the parent is
  151. //html//body//div//div//table[contains(@class, 'csx-new-table')]//tbody//tr
  152. a tr which kind of stands for row in html. This, because GIZ is cooperative and ordered. You need the //tr because that says "search for any tr" which means we are reffering to a list of elements.
  153. #### vars children
  154. for the children it is about to define in xpath syntax, where the text lies that we want to be parsed automatically. child-name, child-link, child-period, child-info and child-sponsor are defined until now.
  155. In future it will be possible to define any variables anywhere and get that fed into the json output.
  156. #### var slow-downlading
  157. slow downloading comes into take when a website uses lazy loading and / or is used by too many users resulting in loading very slow. In this case, the selenium part, as any normal user, runs into problems of timing. Which leads to much longer processing time, when harvesting the lists. Depending on the configuration of the firewall of the server you are harvesting, there may even be a limit by time. And again, we just can act as a lot of different instances and everyone just gets a part of it to download. In this sense, even for future totalitarist system that may come, the freeing of information from big platforms is always and will always be possible in peer to peer systems.