forked from deepanprabhu/duckduckgo-images-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
58 lines (50 loc) · 1.71 KB
/
api.py
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
55
56
57
58
import requests;
import re;
import json;
import pprint;
import time;
def search(keywords, max_results=None):
url = 'https://duckduckgo.com/';
params = {
'q': keywords
};
# First make a request to above URL, and parse out the 'vqd'
# This is a special token, which should be used in the subsequent request
res = requests.post(url, data=params)
searchObj = re.search(r'vqd=(\d+)\&', res.text, re.M|re.I);
headers = {
'dnt': '1',
'accept-encoding': 'gzip, deflate, sdch, br',
'x-requested-with': 'XMLHttpRequest',
'accept-language': 'en-GB,en-US;q=0.8,en;q=0.6,ms;q=0.4',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'accept': 'application/json, text/javascript, */*; q=0.01',
'referer': 'https://duckduckgo.com/',
'authority': 'duckduckgo.com',
}
params = (
('l', 'wt-wt'),
('o', 'json'),
('q', keywords),
('vqd', searchObj.group(1)),
('f', ',,,'),
('p', '2')
)
requestUrl = url + "i.js";
while True:
res = requests.get(requestUrl, headers=headers, params=params);
data = json.loads(res.text);
printJson(data["results"]);
if "next" not in data:
exit(0);
requestUrl = url + data["next"];
time.sleep(5);
def printJson(objs):
for obj in objs:
print "Width {0}, Height {1}".format(obj["width"], obj["height"]);
print "Thumbnail {0}".format(obj["thumbnail"]);
print "Url {0}".format(obj["url"]);
print "Title {0}".format(obj["title"].encode('utf-8'));
print "Image {0}".format(obj["image"]);
print "__________";
search("dora coloring pages");