Klicken Sie hier, um die DMM-API-Spezifikationen anzuzeigen https://affiliate.dmm.com/api/ Es wird nur XML zurückgegeben, sodass Sie es analysieren können oder was auch immer Sie möchten ...
#coding:utf-8
import urllib2
import time
"""
Erhalten Sie Produktinformationen mithilfe der DMM-API
"""
class Dmm:
def __init__(self, api_id, affiliate_id):
"""Konstrukteur"""
self.baseurl = "http://affiliate-api.dmm.com/"
self.proxy_host = None
self.proxy_port = None
self.api_id = api_id
self.affiliate_id = affiliate_id
self.version = "1.0.0"
self.site = 'DMM.co.jp'
self.service = 'digital'
self.operation = 'ItemList'
self.url = None
def setProxy(self, host, port=8080):
"""Proxy festlegen"""
self.proxy_host = host
self.proxy_port = port
def setVersion(self, version):
"""Version einstellen"""
self.version = version
def setSite(self, site):
"""Site festlegen"""
self.site = site
def setService(self, service):
"""Service einstellen"""
self.service = service
def keywordSearch(self, keyword, **options):
"""Nach Artikeln suchen"""
params = options
params["keyword"] = keyword
return unicode(self.sendRequest(params),'euc-jp')
#return self.buildURL(params)
def buildURL(self, params):
""""""
params["api_id"] = self.api_id
params["affiliate_id"] = self.affiliate_id
params["version"] = self.version
params["site"] = self.site
params["service"] = self.service
params["operation"] = self.operation
params["timestamp"] = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
sorted_params = sorted(params.items())
#Erweitern Sie den Hash von Params
request = []
for p in sorted_params:
pair = "%s=%s" % (p[0], urllib2.quote(p[1].encode("euc-jp")))
request.append(pair)
url = self.baseurl + "?" + "&".join(request)
return url
def sendRequest(self, params):
"""Senden Sie eine Anfrage an DMM und geben Sie das abgerufene XML zurück"""
self.url = self.buildURL(params)
if self.proxy_host:
proxy_handler = urllib2.ProxyHandler({"http":"http://%s:%s/" % (self.proxy_host, self.proxy_port)})
opener = urllib2.build_opener(proxy_handler)
else:
opener = urllib2.build_opener()
return opener.open(self.url).read()
def _test():
dmm = Dmm('API-Schlüssel', 'Affi-Code')
print dmm.keywordSearch('sex',hits='100')
if __name__ == '__main__':
_test()
Recommended Posts