0

我正在尝试检索给定 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 响应,但它在我的本地机器上按预期工作

任何帮助将非常感激!

4

3 回答 3

2

不是答案,但希望这可以为其他人节省一个小时尝试这种方法:不出所料,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 代理服务器

于 2013-05-25T22:33:51.673 回答
2

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

于 2015-10-22T18:00:18.097 回答
1

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

于 2013-04-08T18:50:43.037 回答