python如何快速登记凭证_如何在Python中设置AWS凭证

我正在尝试实现一个使用amazonapi的程序。我用过约阿瓦维拉姆做的包装纸。我把我的代码推到github上,并被通知我的amazon,我不应该在代码中显式地包含AWS凭证。我发现一些代码使用boto来访问像AWS的bucket之类的东西,但我不认为我需要使用它。如何在下面的代码中传递凭据而不在代码中显式地写入它们的值?在#windowShopping will take all of the Amazon HTMLs from a data structure and will retrieve all of the used/new prices

import re

import json

import requests

from bs4 import BeautifulSoup

from amazon.api import AmazonAPI

import time

AMAZON_ACCESS_KEY = < my access key >

AMAZON_SECRET_KEY = < my secret key >

AMAZON_ASSOC_TAG = < my user name >

asin_regex = r'/([A-Z0-9]{10})'

isbn_regex = r'/([0-9]{10})'

def get_amazon_item_id(url):

# return either ASIN or ISBN

asin_search = re.search(asin_regex, url)

isbn_search = re.search(isbn_regex, url)

if asin_search:

return asin_search.group(1)

elif isbn_search:

return isbn_search.group(1)

else:

# log this URL

return None

def get_amazon_product_meta(url):

# the input URL is always of amazon

amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

item_id = get_amazon_item_id(url)

if not item_id:

return None

try:

product = amazon.lookup(ItemId=item_id)

except amazon.api.AsinNotFound:

# log this ASIN

return None

except Exception:

return None

# product.price_and_currency returns in the form (price, currency)

# product_price = product.price_and_currency[0]

new_price = product._safe_get_element_text("OfferSummary.LowestNewPrice.FormattedPrice")

used_price = product._safe_get_element_text("OfferSummary.LowestUsedPrice.FormattedPrice")

trade_in_price = product._safe_get_element_text("ItemAttributes.TradeInValue.FormattedPrice")

if new_price or used_price or trade_in_price:

return new_price, used_price, trade_in_price

return Nonesting.Price.FormattedPrice

def unpickle(fileName):

f = open(fileName, 'r')

HTML_Dict = json.load(f)

print(fileName)

f.close()

return HTML_Dict

def pickle(structure,fileName):

f = open(fileName, 'w' )

json.dump(structure,f)

f.close()

def get_prices(urls,newPricesDict, usedPricesDict, tradeInDict):

#iterates through document of book urls

for url in urls:

price = get_amazon_product_meta(urls[url])

newPricesDict[url] = price[0]

usedPricesDict[url] = price[1]

tradeInDict[url] = price[2]

time.sleep(1)

print(url)

print("\t" + str(price))

def main():

newPrices = {}

usedPrices = {}

tradeInPrices = {}

urlDict = unpickle('addresses.dat')

get_prices(urlDict, newPrices, usedPrices, tradeInPrices)

pickle(newPrices, "newPrices.dat")

pickle(usedPrices, "usedPrices.dat")

pickle(tradeInPrices, "tradeInPrices.dat")

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值