-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathquotes-scraper.js
54 lines (46 loc) · 1.7 KB
/
quotes-scraper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const requestPromise = require('request-promise');
const cheerio = require('cheerio');
const request = require('request');
const baseUrl = 'http://quotes.toscrape.com/';
const loginUrl = 'login';
(async () => {
let html = await requestPromise({
url: baseUrl + loginUrl,
method: 'GET',
gzip: true,
resolveWithFullResponse: true,
});
let cookie = html.headers['set-cookie'].map(value => value.split(';')[0]).join(' ');
console.log('cookie', cookie);
let $ = cheerio.load(html.body);
let csrfToken = $('input[name="csrf_token"]').val();
try {
let loginRequest = await requestPromise({
url: baseUrl + loginUrl,
method: 'POST',
gzip: true,
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'quotes.toscrape.com',
'Origin': 'http://quotes.toscrape.com',
'Referer': 'http://quotes.toscrape.com/login',
'Upgrade-Insecure-Requests': '1',
'Cookie': cookie,
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
},
form: {
'csrf_token': csrfToken,
'username': 'admin',
'password': 'admin',
},
resolveWithFullResponse: true,
});
} catch (err) {
cookie = err.response.headers['set-cookie'].map(value => value.split(';')[0]).join(' ');
}
})();