【问题标题】:Pinterest API - returning 403 on EC2 InstancePinterest API - 在 EC2 实例上返回 403
【发布时间】:2013-03-29 19:45:48
【问题描述】:

我正在尝试检索给定 URL 的 Pin 图数。我创建了这个 Python 脚本,它采用两个单独的 URL 并打印出每个 URL 的数量。当我在本地机器上运行这个脚本时,我返回了一个包含 Pin 计数的 200 响应,但是,当我在我的 EC2 实例上运行完全相同的脚本时,我返回了 403 错误。

这是 Python 脚本:

#!/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)上得到的响应:

$ 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) 上运行相同脚本时得到的响应:

$ 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 消息,但我不明白的是 为什么当我从我的 EC2 实例运行脚本时得到 403 响应,但它在我的本地机器上按预期工作。

任何帮助将不胜感激!

【问题讨论】:

  • 您在这方面取得了进展吗?实际上我也遇到了同样的问题,起初我以为是我的脚本(红宝石),但我把它缩小到 ec2 + pinterest。
  • 我让另一个用户在不同区域使用他们的 EC2 实例对其进行了测试,但也被拒绝了。我得出的结论是 Pinterest 正在阻止 AWS IP 地址。

标签: python python-2.7 amazon-ec2 pinterest


【解决方案1】:

不是答案,但希望这可以为其他人节省一个小时尝试这种方法:不出所料,Pinterest 似乎也在阻止来自 tor 出口路由器的请求。

我在同一端点上遇到了同样的问题,并将其范围缩小到 EC2 + Pinterest。我试图通过 tor 路由请求来规避它。

class PinterestService(Service):
    service_url = "http://api.pinterest.com/v1/urls/count.json?callback="
    url_param = 'url'

    def get_response(self, url, **params):
        params[self.url_param] = url

        # privoxy listens by default on port 8118
        # on the ec2 privoxy is configured to forward
        # socks5 through tor like so:
        # http://fixitts.com/2012/05/26/installing-tor-and-privoxy-on-ubuntu-server-or-any-other-linux-machine/

        http_proxy  = "socks5://127.0.0.1:8118"

        proxyDict = { 
          "http"  : http_proxy
        }

        return requests.get(self.service_url, params=params, proxies=proxyDict)

我已经循环通过许多出口路由器,响应始终是{ "status": 403, "message": "Forbidden" }

为了解决问题,我将通过一个私有 http 代理服务器

【讨论】:

  • 我也做了一些研究,我的发现和解决方案与您的一致。 Pinterest 似乎阻止 EC2 访问某些 URL。 API url 是一个,另一个是搜索 URL (pinterest.com/search)。我还通过使用代理解决了这个问题。
【解决方案2】:

这个问题是几年前提出的,我认为当前的答案已经过时了。 EC2 现在运行上述脚本并获得成功响应,无需代理。我在调查自己与 Google App Engine 的类似问题时遇到了这个问题。

【讨论】:

    【解决方案3】:

    Pinterest 可能正在阻止来自亚马逊拥有的 IP 块的请求,从而导致 403: Forbidden 错误。 Pinterest 没有对其 API 的官方支持,因此(我的假设是)他们正在阻止其 API 的最大可能商业用途来源。您可以使用来自非 AWS 提供商的实例对此进行测试。

    【讨论】:

      猜你喜欢
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多