DuckDuckGo-getImage
ナビゲーションに移動
検索に移動
概要
※モジュールではありません。
大量アクセス対策としてUserAgentをそのまま送るか偽装して送信します。BAN対策
コード
class ddg:
@classmethod
def image(cls,query):
try:
headers=autoUserAgent()
token = cls._fetch_token(query,headers=headers)
time.sleep((random.randint(0,3))+(0.1*random.randint(0,10)))
return {"word":query,
"Developer":"https://www.DuckDuckGo.com/",
"Response":cls._fetch_search_urls(query, token,headers=headers)}
except:
return {"word":query,
"Developer":"https://www.DuckDuckGo.com/",
"Response":None}
@classmethod
def image_all(cls,query):
try:
headers=autoUserAgent()
token = cls._fetch_token(query,headers=headers)
time.sleep((random.randint(0,3))+(0.1*random.randint(0,10)))
return {"word":query,
"Developer":"https://www.startpage.com/",
"Response":cls._fetch_search_urls(query, token, what=False,headers=headers)}
except:
return {"word":query,
"Developer":"https://www.startpage.com/",
"Response":None}
@classmethod
def _fetch_token(cls,query, URL="https://duckduckgo.com/",headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"}):
res = requests.post(URL, data={'q': query})
if res.status_code != 200:
return ""
match = re.search(r"vqd='([\d-]+)'", res.text, re.M|re.I)
if match is None:
return ""
return match.group(1)
@classmethod
def _fetch_search_urls(cls,query, token, URL="https://duckduckgo.com/", what=True,headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"}):
query = {
"vqd": token,
"q": query,
"l": "wt-wt",
"o": "json",
"f": ",,,",
"p": "2"
}
urls = []
res = requests.get(URL+"i.js", params=query,headers=headers)
if res.status_code != 200:
return urls
data = json.loads(res.text)
for result in data["results"]:
image = result["image"]
if "http://" in image:
image = result["thumbnail"]
urls.append({
"url":result["url"],
"title":result["title"],
"description":result["title"],
'user_name':result["source"],
"image":[image]
})
if what:
return urls
while "next" in data:
res = requests.get(URL+data["next"], params=query,headers=headers)
if res.status_code != 200:
return urls
data = json.loads(res.text)
for result in data["results"]:
image = result["image"]
if "http://" in image:
image = result["thumbnail"]
urls.append({
"url":result["url"],
"title":result["title"],
"description":result["title"],
'user_name':result["source"],
"image":[image]
})
return urls
使う
api = ddg()
print(api.image("検索したいワード")) #画像1枚
print(api.image_all("検索したいワード")) #多くの画像