3 ms·
Is there any situation where you end up using the type system for interacting with APIs but also have to webscrape on the same service? I am interested in hear
by bryanrasmussen 4y ago
Is there any situation where you end up using the type system for interacting with APIs but also have to webscrape on the same service?
I am interested in hearing more about this as maybe I should switch from Node to Deno for a scraping project I have.
- culi 4y agoI haven't done something like that yet, but I'm sure there would be. Most webscraping libraries have good `@types` definitions now though which really comes in handy for at least using the tools You can probably try it in a few minutes if you already have Deno installed. Make a new file. Here's a quick example script ```ts /* run with `deno run --allow-net demo.ts` */ import { DOMParser } from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'; const API_URL = 'https://subsidytracker.goodjobsfirst.org/parent/tesla-inc'; const main = async (args: string[]) => { const resp = await fetch(API_URL); const text = await resp.text(); const html = new DOMParser().parseFromString(text, 'text/html'); console.log( 'Tesla subsidies:', [ ...html.querySelectorAll('table:first-of-type > tbody:first-of-type > tr') ].map(tr => { const [name, value] = [...tr.querySelectorAll('td')]; return { name: name.textContent, value: value.textContent }; }) ); }; main(Deno.args); ``` save that file and run: deno run --allow-net demo.ts