Skip to content

Instantly share code, notes, and snippets.

@kaniket7209
Last active November 1, 2021 13:13
Show Gist options
  • Save kaniket7209/690c3a1aa3987fc6abf10f347d572e64 to your computer and use it in GitHub Desktop.
Save kaniket7209/690c3a1aa3987fc6abf10f347d572e64 to your computer and use it in GitHub Desktop.
Practical Introduction to Web Scraping with Node.js | Cheerio Request
1. What is Request Response Cycle .How it works ?
Answer: This is a kind of cycle where a user gives a client a URL and the client builds a request for information, generated by a server.
Once built ,that response is sent back to the client in the requested format, to be rendered by the user.
2. What are the basic steps of Web-Scrapping Process?
Answer: The steps are:
a) Firstly, with the help of Node.js modules we send the request to browser/machine.
b) Browser then in response returns a html file.
c) We will parse/read with the help of node.js modules .
d) Extract the data from it in then display it in a organised format such a excel,pdf ,etc.
3. What is the use of cheerio?
Answer: To parse/read the response received in html and then extract the data out of it with the help of cheerio modules.
4. Why we use Async(fs.readFile) inspite of normal fs.readFileSync to read the files and execution?
Answer: If the execution takes time to execute then the other functions below that have to wait for the execution of the above function . So suppose if the function returns Time Limit exceed then the functions below that will also be unable to execute so to overcome this issue async is used.
Now if the execution takes time , then the full code or browser will not have to wait for it. It will execute the part of the code that is running
5. What is the syntax of request function with call back.
Answer: request('https://www.worldometers.info/coronavirus/', cb);
function cb(error, html) {
if (error) {
console.log("Page 404 not found");
} else {
console.log('body:', html); // Print the HTML for the Google homepage.
}
}
6. What is the syntax for async function, show the code.
Answer: fs.readFile("abc.txt", cb); // sbc.txt has text "Hiiii"
function cb(error, response) {//response -> data
console.log( ""+response ); // Print the data if a response is received , here it will print Hiiii
}
1. Which of the following gives the biggest size of text.
a) <h1>Hiii</h1>
b) <h>Hii</h>
c) <h8>Hii</h8>
d) <h6>Hii</h6>
Answer: d) <h6>Hii</h6>
2. Ordered list can be used with the help of
a) <ol><li></li></ol>
b) <ol></ol>
c) <ul></ul>
d) <ul><li></li></ul>
Answer: a) <ol><li></li></ol>
3. What is the shortcut to write <p></p> 3 times in html
a) p+3
b) p*3
c) <p>*3
d) (p)*3
Answer: b) p*3
4. Table in html comprises of
a) thead & tbody
b) head & body
c) head & thead
d) body & tbody
Answer: a) thead & tbody
5. What is the tag for element selector . Tag to select all paragraphs at a once
a) el
b) t
c) p
d) .p
Answer: c) p
6. How can we select the descendents in html.
a) div p
b) div.p.
c) div>p
d) div p.
Answer: a) div p
7. How can we select the childrens, take an example:
<div><span><p><span>Hii</span></p></span></div> : Block 1
<div><p><span>Select me</span></p></div>
Now, if we want to change the colour of only "Select me" how can we select it
a) div p span
b) div.p.span.
c) div>p>span
d) div span p span
Answer: c) div>p>span
8. What is the selector for multiple class ?
a) m1>m2
b) m1.m2
c) m1 m2
d) None of the above
Answer: b) m1.m2
9. What is the slector for descendent classes?
a) .c1 .span .c2
b) c1 span c2
c) .c1 span .c2
d) c1 >span >c2
Answer: c) .c1 span .c2
10. Selector of Class with children combinator
<div class="c1"><p>Select me</p></div>
<div class="c1"><span><p>Hii</p></span></div>
a) div c1 p
b) div.c1>p
c) div.c1.p
d) div>c1>p
Answer: b) div.c1>p
11. How can we select the id ,
<p class="s1">Random</p>
<p class="s2">Random</p>
<p class="s2" id="Select-me">Random</p>
a) #id
b) #Select-me
c) #Random
d) s2 id
Answer: b) #Select-me
12. How to select the attributes that is not visible such as button
</body><form>
<input type="button" value="Click me">
<input type="button" value="Select me">
</form><body>
a) input(value="Select me")
b) input value("Select me")
c) input[value = "Select me"]
d) input[value]="Select me"
Answer: c) input[value = "Select me"]
13. Do we need to install request module ?
a) No , its already provided with the node.js just require it
b) Yes, first install it with npm then require it
c) Download extentoin for it and then require
d) None of the above
Answer: b) Yes, first install it with npm then require it
14. Do we need to install cheerio module ?
a) No , its already provided with the node.js just require it
b) Yes, first install it with npm then require it
c) Download extentoin for it and then require
d) None of the above
Answer: b) Yes, first install it with npm then require it
15. What will be the return type od=f ans in following code
let selTool = cheerio.load(html);
let ans = selTool("hi");
a) string
b) array
c) number
d) None of the above
Answer: b) array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment