LoginSignup
26
21

More than 5 years have passed since last update.

AmazonのAPIを使って、アフィリエイトリンクの入った商品リンクを自動生成

Posted at

やったこと

  • アフィリエイトをやるならAmazonだよね
  • これ、自動作成したい人たくさんいるんじゃないかな(ツイッターのbotとか、記事作成とか)
  • なので、これを自動化できる仕組みを作ったよというお話

完成図

  • localserverを起動すると、Amazon Advertising APIからデータを取得した結果がHTMLに出力される

image.png

手順

  • Amazon Advertising APIから、セール情報を取得
  • FlaskでHTML表示
  • bootstrapで加工

ソースコード

  • 詳しいことはブログで!
amazon.py

import bottlenose
from bs4 import BeautifulSoup
import time
from random import randint
import requests
import os
from flask import Flask

AMAZON_ACCESS_KEY = ""
AMAZON_SECRET_KEY= ""
AMAZON_ASSOC_TAG= ""


#1 

app = Flask(__name__)

#2

@app.route('/')
def parseHTML():

    responses = getResponses()
    content = """
            <!doctype html>
            <html lang="en">
              <head>
                <title>Hello, world!</title>
                <!-- Required meta tags -->
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

                <!-- Bootstrap CSS -->
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
              </head>
              <body>
                <div class='container'>
            """
    for response in responses:
        try:
            content += "<div class='row'>"
            content +=      "<div class='col-md-10 col-md-offset-1'>"
            content +=          "<div><h3>{}</h3></div>\n\n".format(response.title.text)
            content +=          "<div>著者:{}</div>".format(response.author.text)
            content +=          "<div><a href = '{}'><img src = '{}'></a></div><!-- 画像-->\n".format(response.detailpageurl.text, response.largeimage.url.text)
            content +=          "<div><blockquote class='blockquote'><p class='mb-0'>{}<p></blockquote></div><!-- 内容-->\n".format(response.content.text)
            content +=          "<div><a href='{}'>詳細はこちら</a></div><!-- リンク-->\n".format(response.detailpageurl.text)
            content +=      "</div>"
            content+= "</div>"
        except Exception as e:
            content +=      "</div>"
            content+= "</div>"
            print(e)

    content += """
            </div>
            <!-- Optional JavaScript -->
            <!-- jQuery first, then Popper.js, then Bootstrap JS -->
            <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
          </body>
        </html>
        """
    return content

#3

def getResponses():
    while True:
        try:        
            amazon = bottlenose.Amazon(AMAZON_ACCESS_KEY,AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region='JP')
            response = amazon.ItemSearch(
                SearchIndex = 'Books',
                BrowseNode = 3550442051,
                ResponseGroup= 'Large'
                    )
            soup = BeautifulSoup(response,"lxml")
            print("データの取得に成功しました")
            return (soup.findAll("item"))
            #return (soup.prettify())
        except: #503エラーが出たら再取得する
            print("再取得しています....")
            time.sleep(3)

#4

if __name__ == '__main__':
    app.run()

参考

【Python】Amazonのセール商品をAmazon Product Advertising APIで抽出し、自動記事作成をやってみた|Review of My Life

26
21
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
26
21