Homebrew Python Part 3-Amazon Product Search Program

I like the area where I can reach the itch.

Please check the genre specification of index = "All" at the link below. Specify product category (SearchIndex)

I think this is pretty good.

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 =Genre such as 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('Select a number:'))
                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("Search word:"),index="All")
    webbrowser.open(A.url)

if __name__ == '__main__':
     main()

Recommended Posts

Homebrew Python Part 3-Amazon Product Search Program
Homebrew Python --Youtube Search Program
Homebrew search from python module name (ShellScript)
[Python] Shopping program
QGIS + Python Part 2
QGIS + Python Part 1
Python: Scraping Part 1
Python3 Beginning Part 1
Python: Scraping Part 2
Anomaly detection with Amazon Lookout for Vision Part 2 (Python3.6)
Sequential search with Python
Python Exercise 1-Breadth-first search
[Python] Search (itertools) ABC167C
Binary search in Python
homebrew python environment construction
[Python] Search (NumPy) ABC165C
Binary search (python2.7) memo
[Python] Binary search ABC155D
Python basic memorandum part 2
python bit full search
Linear search in Python
Python basic memo --Part 2
Binary search with python
Binary search with Python3
Search Twitter using Python
Binary search in Python (binary search)
Python basic memo --Part 1
Output product information to csv using Rakuten product search API [Python]
Aggregate and analyze product prices using Rakuten Product Search API [Python]