【发布时间】: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