How can i get data from any website or dom in Angular 4


#1

i want to make a app which show the result of my university now how can i get the data from my university website ?Is there any way to do that ?please help


#2

You’ll need to use Http requests to fetch data, as a general response.
I think what you are referring to by saying ‘from DOM’ is your university website does not have any API support. In that case, all you can do is ‘screen scrape’ - that is do an HTTP GET on the web page and parse its contents to extract data.

Notes: (assuming you plan to screen scrape)

  1. Make sure you are compliant to the “Terms of Use” of the website (screen scraping could be illegal depending on conditions/location/etc - google around)
  2. Remember that if you do parse the web page, any small change they do may break your code
  3. If you must screen scrape, use a forgiving parser (that is, most websites will have HTML coding errors, don’t use a parser that borks for every small transgression)
  4. There are intermediary services that parse website and expose APIs to you - I’ve never tried them
  5. Try not to use regex (actually, this is true only if you are trying to do complex tag matching. I find regex to be ideal if I’m just globbing for very specific data and not trying to match tag start/ends)

#3

how can i use forgiving parser in angular two i am getting error could you help me ?
how should i import it after installing it ?


#4

Actually that link was just an example - if it doesn’t have typescript bindings, you’ll have to import it in index.html and declare it in your ts code.
There are other options too like DOMParser https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
There is also parse5 - I’ve heard many good things - haven’t used it

You’ll have to experiment - on what works best for you. I’ve used DOMParser in the past and it worked for me.