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.

329 lines
15 KiB

11 months ago
11 months ago
11 months ago
7 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. - [javascript](#javascript)
  21. - [var jsdomain](#var-jsdomain)
  22. - [vars jslink and jsiteration](#vars-jslink-and-jsiteration)
  23. - [example1 jslink and jsiteration](#example1-jslink-and-jsiteration)
  24. - [display](#display)
  25. - [another tip](#another-tip)
  26. - [var parent](#var-parent)
  27. - [example1 parent](#example1-parent)
  28. - [vars children](#vars-children)
  29. - [var slow downloading](#var-slow-downloading)
  30. # Introduction
  31. The fdb-spider was made to gather data from Websites in an automated way.
  32. The Website to be spidered has to be a list of Links.
  33. Which makes the fdb-spider a web spider for most Plattforms.
  34. The fdb-spider is to be configured in a .yaml file to make things easy.
  35. The output of the fdb-spider is in json format to make it easy to input
  36. the json to other programs.
  37. At its core, the spider outputs tag search based entries
  38. It works together with the fdb-spider-interface.
  39. In Future, the spider will be extended by the model Sauerkraut.
  40. An !open source! Artificial Neural Network.
  41. # Installation
  42. FIRST install systemwide requirements with your package manager
  43. ```
  44. # apt based unixoids
  45. apt install xvfb
  46. apt install chromium
  47. apt install chromium-webdriver
  48. # pacman based unixoids
  49. pacman -S xorg-server-xvfb
  50. pacman -S xorg-server-xephyr
  51. pacman -S chromium
  52. ```
  53. THEN create a python3 virtualenv on your favourite UNIX Distribution
  54. with the command
  55. ```
  56. git clone https://code.basabuuka.org/alpcentaur/fdb-spider
  57. cd fdb-spider
  58. virtualenv venv
  59. source venv/bin/activate
  60. pip install -r requirements.txt
  61. ```
  62. # Usage
  63. Use it step by step. First care for the htmls of the lists of the links.
  64. Then care for getting the first json output from the first layer of html
  65. pages.
  66. Copy the two examples to the file name, in which they will be
  67. input to the spider
  68. ```
  69. cp main.py_example main.py
  70. cp spiders/config.yaml_example spiders/config.yaml
  71. ```
  72. ## Configuration File Syntax
  73. The configuration file with working syntax template is
  74. ```
  75. /spiders/config.yaml
  76. ```
  77. Here you can configure new websites to spider, referred to as "databases".
  78. link1 and link2 are the links to be iterated.
  79. The assumption is, that every list of links will have a loopable structure.
  80. If links are javascript links, specify js[domain,link[1,2],iteration-var-list].
  81. Otherwise leave them out, but specify jsdomain as 'None'.
  82. You will find parents and children of the entry list pages.
  83. Here you have to fill in the xpath of the entries to be parsed.
  84. In the entry directive, you have to specify uniform to either TRUE or FALSE.
  85. Set it to TRUE, if all the entry pages have the same template, and you
  86. are able to specify xpath again to get the text or whatever variable you
  87. like to specify.
  88. In the entry_unitrue directive, you can specify new dimensions and
  89. the json will adapt to your wishes.
  90. Under the entry-list directive this feature has to be still implemented.
  91. So use name, link, javascript-link, info, period and sponsor by commenting
  92. in or out.
  93. If javascript-link is set (which means its javascript clickable),
  94. link will be ignored.
  95. Set it to FALSE, if you have diverse pages behind the entries,
  96. and want to generally get the main text of all the different links.
  97. For your information, the library trafilature is used to gather the
  98. text generally for further processing.
  99. ## Efficient Xpath Copying
  100. When copying the Xpath, most modern Webbrowsers are of help.
  101. In Firefox (or Browsers build on it like the TOR Browser) you can use
  102. ```
  103. strl-shift-c
  104. ```
  105. to open the "Inspector" in "Pick an element" mode.
  106. When you click on the desired entry on the page,
  107. it opens the actual code of the clicked element in the html search box.
  108. Now make a right click on the code in the html search box, go on "Copy",
  109. and go on XPath.
  110. Now you have the XPath of the element in your clipboard.
  111. When pasting it into the config, try to replace some slashes with double
  112. slashes. That will make the spider more stable, in case the websites
  113. html/xml gets changed for maintenance or other reasons.
  114. ## Step By Step Guide
  115. Start with an old Configuration that is similar to what you need.
  116. There are Three Types of Configurations:
  117. The first Type is purely path based. An example is greenjobs.de.
  118. The second Type is a mixture of path and javascript functions, giz is an example for this Type.
  119. The third Type is purely javascript based. An example is ted.europe.eu.
  120. Type 1:
  121. Start with collecting every variable.
  122. From up to down.
  123. ### var domain
  124. domain is the variable for the root of the website.
  125. In case links are glued, they will be glued based on the root.
  126. ### var entry list
  127. Now come all the variables regarding the entry list pages.
  128. #### var link and iteration
  129. In Pseudo Code, whats happening with these three variables is
  130. ```
  131. for n in iteration var list:
  132. get(link1 glued to n glued to link2)
  133. ```
  134. So if you are on the no javascript side of reality, you are lucky. Thats all needed to get the collection of links.
  135. #### example1 link
  136. Lets say we go on greenjobs.de.
  137. We go on search without search query. To get the biggest displayed output, in best case a table of everything the site has listed.
  138. https://www.greenjobs.de/angebote/index.html?s=&loc=&countrycode=de&dist=10&lng=&lat=
  139. is the resulting url.
  140. So now we navigate through the pages.
  141. 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.
  142. #### example2 link
  143. 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:
  144. https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT=2
  145. Going on the next side again, we get the url:
  146. https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT=3
  147. So now we already see the pattern, that any and every machine generated output cant hide.
  148. RSULT=1 .... we put it in the url bar of the browser
  149. https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT=1
  150. and get to the first pages.
  151. Which leads to the following variables, considering that there were 6 pages:
  152. * link1 = "https://ausschreibungen.giz.de/Satellite/company/welcome.do?method=showTable&fromSearch=1&tableSortPROJECT_RESULT=2&tableSortAttributePROJECT_RESULT=publicationDate&selectedTablePagePROJECT_RESULT="
  153. * link2 = ""
  154. * iteration-var-list = "[1,2,3,4,5,6]"
  155. #### javascript
  156. 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 only get html gibberish. To get the desired info with the help of this program, you have the possibility to give the config.yaml paths of clickable items. The spider will open an axctual browser and click through the pages that start to exist.
  157. #### var jsdomain
  158. If jsdomain is "None" (And here it is important to use None and not NONE), it will try to get the domains and download elements based on the gets with a variety of libraries.
  159. But if you have a javascript situation, where the first html are already javascript generated kaos without xml to parse.. then you need to put an url here. By putting the url, the spider will open that website with a virtual graphical browser (using selenium), wait for the javascript to load, and by clicking on jslink, go through the pages.
  160. In pseudo code that means, if you fill out the variable jsdomain in the config.yaml, the spider will do
  161. ```
  162. for i in jsiteration-var-list:
  163. click on the jslink string glued to i glued to jslink2
  164. ```
  165. #### vars jslink and jsiteration
  166. In jslink1 and jslink2 you have to put the xpath of the button that clicks to the next site of entry links to download.
  167. Sometimes the xpath changes, after the new js content got loaded. That is where the jsiteration-var-list comes in. And like this, you define the button that gets klicked every site. Sometimes it stays the same, then you just need an occurence of the same number exactly the same times as in the var-iteration, which will define how many pages will be downloaded generally. The var iteration-var-list defines the folder structure of the json output files.
  168. 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, so put these accordingly in the jslink and jsiteration variables.
  169. 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. How to do this, in general if you use any of the js related variables instead of setting "NONE" (important: and not "None"), will be described in the paragraph display. It is very useful to debug js related configs.
  170. 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.
  171. #### example1 jslink and jsiteration
  172. So let us consider evergabe-online as an example.
  173. ```
  174. evergabe-online:
  175. domain: 'https://www.evergabe-online.de/'
  176. entry-list:
  177. link1: 'https://www.evergabe-online.de/search.html?101-1.-searchPanel>
  178. link2: '-pageLink'
  179. jsdomain: 'https://www.evergabe-online.de/search.html'
  180. jslink1: '/html/body/div[8]/main/div[4]/div/div/div[2]/table/thead/tr[1]/td/div[2]/div/span['
  181. jslink2: ']'
  182. jsiteration-var-list: "[1,2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
  183. iteration-var-list: "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
  184. ```
  185. Go on jsdomain https://www.evergabe-online.de/search.html.
  186. You will see the table we want to spider.
  187. Open the inspector, and have a look at the button to get to the next site.
  188. Its xpath is '/html/body/div[8]/main/div[4]/div/div/div[2]/table/thead/tr[1]/td/div[2]/div/span[1]'
  189. Now we click on it. On page two, the button to click us to page three has the xpath:
  190. '/html/body/div[8]/main/div[4]/div/div/div[2]/table/thead/tr[1]/td/div[2]/div/span[2]'
  191. From page 5 on, the button to get to the next pages stays
  192. '/html/body/div[8]/main/div[4]/div/div/div[2]/table/thead/tr[1]/td/div[2]/div/span[6]'
  193. until the end.
  194. #### display
  195. When you run the spider with js spidering enabled, in fdb_spider.py a display will get created. If you open nano, and press crtl+w, you can type display and enter. This will bring you to the lines of code, generating the display.
  196. Watch out for the line
  197. ```
  198. display = Display(visible=0, size=(1200, 800))
  199. ```
  200. If you change visible=0 to visible=1 here, the spider will run with actually open a viewable browser on the workspace.
  201. This line is present two times in the code. One for downloading the pages with the links, and one for downloading the pages of/behind the links.
  202. After finding and jumping to "display" with ctrl-w, issue alt+w to get to the next display variable definition.
  203. #### another tip
  204. In main.py, where the spiders code gets loaded, you have also a function available called
  205. ```
  206. spider.find_config_parameter(list_of_fdbs)
  207. ```
  208. This function helps you to find the right config parameters, because it shows you what you get. That can be a bit tricky, because if you get nothing, it does not really help you. But when you have the first ones running, it shows you exactly that: That its always possible and no magic needed.
  209. #### var parent
  210. 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.
  211. From the last child, when it is selected in the code view of the Inspector, do a left click on it.
  212. Select copy --> full xpath.
  213. Now copy paste the xpath to the parent variable, and put a double slash in front of the last element in the route.
  214. #### example1 parent
  215. For the List of the Gesellschaft für internationale Zusammenarbeit the last child before the parent is
  216. //html//body//div//div//table[contains(@class, 'csx-new-table')]//tbody//tr
  217. 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.
  218. #### vars children
  219. 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.
  220. In future it will be possible to define any variables anywhere and get that fed into the json output.
  221. #### var javascript link
  222. In case the whole website to spider is javascript generated gibberish, there is another possibility for you. To find out if the website is generated gibberish not containing your payload, just search in the outputed pages for the child name etc. If you do not find them, or directly see on the html pages no real xml, try to download your sites with jsdomain, and go up again to the paragraphs before. For the actual link child, the spider can javascript style clicking download the htmls behind the links already while downloading the entry list htmls with javascript.
  223. For that to happen, you can define the javascript link that needs to be clicked in xpath syntax. If it becomes a pop up, which source code needs to be processed, or if it becomes an actual page the source code will be parsed, or if the clickable link refers to a pdf, the spider will handle all situations and output the resulting text in json under spiders/output.
  224. #### var slow downlading
  225. 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.