LoginSignup
4
8

More than 5 years have passed since last update.

自作Pythonその3 - Amazonの商品検索プログラム

Posted at

痒いところに手が届く範囲がすきなんです。

index = "All" のジャンル指定は以下のリンク先で確認してください。
商品カテゴリーの指定(SearchIndex)

これ結構さいつよだと思います。

import bottlenose
from bs4 import BeautifulSoup
import webbrowser

class Content:
    pass

class Amazon():
    def __init__(self):
        AMAZON_ACCESS_KEY_ID = ""
        AMAZON_SECRET_KEY    = ""
        AMAZON_ASSOC_TAG     = ""
        self.amazon = bottlenose.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region="JP")
    def search(self,query,index="All"):# index = Videoなどジャンル
        response = self.amazon.ItemSearch(SearchIndex=index, Keywords=query, ItemPage=1, ResponseGroup="Large")
        xml  = response.decode('utf-8','strict')
        soup = BeautifulSoup(xml, "html5lib")
        data = soup.findAll('item')
        for d in data:
            url   = d.detailpageurl.text
            title = d.title.text.replace("[","【").replace("]","】")
            con       = Content()
            con.asin  = d.asin.text
            con.amazonjs= '[amazonjs asin="%s" locale="JP"]' % d.asin.text
            con.title = title
            con.url   = url
            con.link  = '<a href="%s">%s</a>' % (url,title)
            con.price = d.formattedprice.text
            con.img   = d.largeimage.url.text
            yield con

class SelectAmazon():
    def __init__(self,query,index="All"):
        data = list(Amazon().search(query,index=index))
        while True:
            for i,datum in enumerate(data):
                print("%s:%s" % (i,datum.title))
            try:
                put = int(input('番号を選択:'))
                break
            except:
                continue
        self.asin  = [datum.asin for datum in data][put]
        self.title = [datum.title for datum in data][put]
        self.url   = [datum.url for datum in data][put]
        self.price = [datum.price for datum in data][put]
        self.img = [datum.img for datum in data][put]
        self.amazonjs = [datum.amazonjs for datum in data][put]
        self.link  = [datum.link for datum in data][put]

def main():
    A = SelectAmazon(input("検索ワード:"),index="All")
    webbrowser.open(A.url)

if __name__ == '__main__':
     main()

4
8
8

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
4
8