Pinterest的API - 在EC2实例返回403实例、API、Pinterest

2023-09-11 09:44:56 作者:一曦時光ゞ涼透半盏流年

我试图找回引脚数对于一个给定的URL。我创造了这个Python脚本,它有两个不同的URL,并打印出销的量为每。当我在我的本地计算机上运行此脚本,我返回200响应包含引脚数,但是,当我在我的EC2实例上运行完全一样的剧本,我返回403错误。

I'm attempting to retrieve the number of Pins for a given URL. I created this Python script, which takes two separate URLs and prints out the amount of Pins for each. When I run this script on my local machine I'm returned a 200 response containing the Pin count, however, when I run the exact same script on my EC2 instance I'm returned 403 error.

下面是Python的脚本:

Here is the Python script:

#!/usr/bin/python

import requests

# Pinterest API
pinterest_endpoint = "http://api.pinterest.com/v1/urls/count.json?callback=&url="

# Emulate a SQL Query result (id, url)
results = [(1, "http://allrecipes.com/recipe/easter-nests/detail.aspx"), (2, "http://www.foodnetwork.com/recipes/ina-garten/maple-oatmeal-scones-recipe/index.html")]

# Cycle thru each URL
for url in results:
    # Print URL details
    print url[0]
    print url[1]
    print type(url[0])
    print type(url[1])
    print "Downloading: ", url[1]

    # Create Complete URL
    target_url = pinterest_endpoint + url[1]
    print target_url

    # Hit Pinterest API
    r = requests.get(target_url)
    print r
    print r.text
    # Parse string response
    start = r.text.find('\"count\"')
    end = r.text.find(',', start+1)
    content = len('\"count\"')
    pin_count = int(r.text[(start+content+1):end].strip())
    print pin_count

这是我得到我的本地机器的响应(Ubuntu的12.04):

This is the response I get on my local machine (Ubuntu 12.04):

$ python pin_count.py
1
http://allrecipes.com/recipe/easter-nests/detail.aspx
<type 'int'>
<type 'str'>
Downloading:  http://allrecipes.com/recipe/easter-nests/detail.aspx
http://api.pinterest.com/v1/urls/count.json?callback=&url=http://allrecipes.com/recipe/easter-nests/detail.aspx
<Response [200]>
({"count": 997, "url": "http://allrecipes.com/recipe/easter-nests/detail.aspx"})
997
2
http://www.foodnetwork.com/recipes/ina-garten/maple-oatmeal-scones-recipe/index.html
<type 'int'>
<type 'str'>
Downloading:  http://www.foodnetwork.com/recipes/ina-garten/maple-oatmeal-scones-recipe/index.html
http://api.pinterest.com/v1/urls/count.json?callback=&url=http://www.foodnetwork.com/recipes/ina-garten/maple-oatmeal-scones-recipe/index.html
<Response [200]>
({"count": 993, "url": "http://www.foodnetwork.com/recipes/ina-garten/maple-oatmeal-scones-recipe/index.html"})
993

这是我得到的回应时,我在我的EC2实例(Ubuntu的)运行相同的脚本:

This is the response I get when I run the same script on my EC2 instance (Ubuntu):

$ python pin_count.py
1
http://allrecipes.com/recipe/easter-nests/detail.aspx
<type 'int'>
<type 'str'>
Downloading:  http://allrecipes.com/recipe/easter-nests/detail.aspx
http://api.pinterest.com/v1/urls/count.json?callback=&url=http://allrecipes.com/recipe/easter-nests/detail.aspx
<Response [403]>
{ "status": 403, "message": "Forbidden" }
Traceback (most recent call last):
  File "cron2.py", line 32, in <module>
    pin_count = int(r.text[(start+content+1):end].strip())
ValueError: invalid literal for int() with base 10: 'us": 403'

我明白为什么它吐出ValueError错误消息,我不明白的是为什么我得到一个403响应,当我从我的EC2实例中运行的脚本,但它工作正常,从我的本地机器。

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

这个问题被提起,几年前,和目前的答案,我认为是过时的。 EC2现在运行与无需一个代理到成功的响应上面的脚本。我碰到这个问题,在调查自己与谷歌的App Engine类似的问题。

This question was filed a few years ago, and the current answer I believe is out of date. EC2 now runs the above script with a successful response without the need for a proxy. I came across this question while investigating my own similar issue with Google App Engine.