2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonでDMM(FANZA)のAPIにリクエストを送信する方法

Posted at

もくじ

  • きっかけ
  • ソースコード

きっかけ

アフィリエイトなんかではよく利用されているDMMのAPI。
これにpythonでリクエストを送信して検索結果を取得してみたいと思ってコードを書いてみたので、メモしておきます。
(実際に書いて利用したのは結構前です)

ソースコード

同人作品情報を取得するサンプルです。

# dayUtil
import datetime
# htmlUtils
import requests
from urllib.request import urlopen
# JsonParser
import json

# リクエスト電文で使用するドメイン
DOMAIN = "https://api.dmm.com/affiliate/v3/ItemList"
# リクエスト電文で使用するAPI_ID
API_ID = "xxxxx"
# リクエスト電文で使用するAFFILIATE_ID
AFFILIATE_ID = "xxxxx"
# リクエスト電文で使用するSORT_VALUE
SORT_VALUE = "date"
# リクエスト電文で使用するOUTPUT
OUTPUT = "json"
# リクエスト電文で使用するSITE
SITE = "DMM.R18"

# ==================================================
# html生成
# ==================================================
def create():
    jsonObject = requestToApi(10, 30, "キーワード", "digital_doujin", "doujin")

# ==================================================
# APIへリクエスト送信
# ==================================================
def requestToApi(hitsNum, dateFromNum, keyword, floor, service):
    response = requests.get(createRequest(hitsNum, dateFromNum, keyword, floor, service))
    jsonObject = response.json()

    return jsonObject

# ==================================================
# リクエスト生成
# ==================================================
def createRequest(hitsNum, dateFromNum, keyword, floor, service):
    txt = DOMAIN + "?" \
    + createQueryParam("api_id", API_ID) + "&" \
    + createQueryParam("affiliate_id", AFFILIATE_ID) + "&" \
    + "hits=" + str(hitsNum) + "&" \
    + createQueryParam("sort", SORT_VALUE) + "&" \
    + createQueryParam("keyword", keyword) + "&" \
    + createDateTxt(dateFromNum) + "&" \
    + createQueryParam("output", OUTPUT) + "&" \
    + createQueryParam("site", SITE) + "&" \
    + createQueryParam("floor", floor) + "&" \
    + createQueryParam("service", service)

    return txt

# ==================================================
# クエリパラメータを生成
# ==================================================
def createQueryParam(key, value):
    return key + "=" + value
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?