获取亚马逊评论的CSV文件,使用Python和AWS。

huangapple go评论62阅读模式
英文:

Get Amazon reviews in a csv file via Python and AWS

问题

这是我的实际代码,但我无法连接到数据库(我不理解,也找不到如何配置AWS的文档)。我收到了错误410。

寻找指南和建议。谢谢。

英文:

Here is my actual code but I can't get through the data base (I don't understand and didn't find documentation how to configure AWS). I got error 410.

Just looking for guide and advices. Thanks.

import bottlenose
import boto3
from bs4 import BeautifulSoup
import pandas as pd
 
AWS_ACCESS_KEY_ID = '146h760vij'
AWS_SECRET_ACCESS_KEY = 'gikbTElBmA6BexZt8eWKF23apWBbY5fx3Uj2Fpxr'
AWS_ASSOCIATE_TAG = 'testest'
 
amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG)
 
def get_product_reviews(asin):
    reviews_data = []
    
    try:
        response = amazon.ItemLookup(ItemId=asin, ResponseGroup='Reviews', TruncateReviewsAt=1000)
        
        soup = BeautifulSoup(response, 'xml')
    
        reviews = soup.find_all('Review')
    
        for review in reviews:
            review_id = review.ReviewId.text
            rating = review.OverallRating.text
            review_text = review.Content.text
            
            reviews_data.append({
                'Review ID': review_id,
                'Rating': rating,
                'Review Text': review_text
            })
    
    except Exception as e:
        print("An error occurred during the API request:", e)
    
    return reviews_data

asin = 'B09N9JKM8S'
reviews = get_product_reviews(asin)

df = pd.DataFrame(reviews)

df.to_csv('reviews.csv', index=False)




</details>


# 答案1
**得分**: 1

错误410表示给定ASIN的产品不再可用或不存在于Amazon产品广告API中因此首先您应该验证您使用的ASIN是否正确并且仍然存在于Amazon上

在配置AWS方面您需要设置您的AWS凭据和关联标签在您的AWS账户中以下是您可以执行的步骤

1. 如果尚未创建AWS账户请先创建一个AWS账户
2. 登录到AWS管理控制台
3. 转到IAM仪表板并创建一个新的具有程序访问权限的IAM用户
4. 给IAM用户授予访问Amazon产品广告API所需的权限
5. 记下IAM用户的访问密钥ID和秘密访问密钥
6. 通过注册Amazon联盟计划来设置一个关联标签
7. 在您的Python代码中使用访问密钥ID秘密访问密钥和关联标签进行API调用

您需要使用boto3包来配置AWS凭据并进行API调用以下是如何使用boto3配置AWS凭据的示例

```python
import boto3

AWS_ACCESS_KEY_ID = 'your_access_key_id'
AWS_SECRET_ACCESS_KEY = 'your_secret_access_key'

session = boto3.Session(
    aws_access_key_id=AWS_ACCESS_KEY_ID,
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
    region_name='us-west-2'  # 替换为适用于您用例的适当地区
)

# 为Amazon产品广告API创建客户端
amazon = session.client('sqs')

your_access_key_idyour_secret_access_key替换为您的实际AWS凭据。此外,根据您的用例,将'us-west-2'替换为适当的地区。您可以在AWS文档中找到可用地区的列表。

英文:

Error 410 indicates that the product with the given ASIN is no longer available or does not exist in the Amazon Product Advertising API. So firstly, you should verify that the ASIN you are using is correct and still exists on Amazon.

In terms of configuring AWS, you will need to set up your AWS credentials and associate tag in your AWS account. Here's how you can do it:

  1. Create an AWS account if you haven't already done so.
  2. Log in to the AWS Management Console.
  3. Go to the IAM dashboard and create a new IAM user with programmatic
    access.
  4. Give the IAM user the necessary permissions to access the Amazon
    Product Advertising API.
  5. Take note of the Access Key ID and Secret Access Key for the IAM
    user.
  6. Set up an Associate Tag by signing up for the Amazon Associates
    program.
  7. Use the Access Key ID, Secret Access Key, and Associate Tag in your
    Python code to make API calls.

You will need to use the boto3 package to configure AWS credentials and make API calls. Here's an example of how to configure AWS credentials using boto3:

import boto3
AWS_ACCESS_KEY_ID = &#39;your_access_key_id&#39;
AWS_SECRET_ACCESS_KEY = &#39;your_secret_access_key&#39;
session = boto3.Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=&#39;us-west-2&#39;  # replace with the appropriate region
)
# create a client for the Amazon Product Advertising API
amazon = session.client(&#39;sqs&#39;)

Replace your_access_key_id and your_secret_access_key with your actual AWS credentials. Also, replace 'us-west-2' with the appropriate region for your use case. You can find a list of available regions in the AWS documentation.

huangapple
  • 本文由 发表于 2023年5月13日 19:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定