我試圖檢索給定URL的引腳數。我創建了這個Python腳本,它帶有兩個單獨的URL併爲每個URL打印出大量的引腳。當我在本地機器上運行此腳本時,我返回了一個包含引腳數的200響應,但是,當我在EC2實例上運行完全相同的腳本時,我返回了403錯誤。Pinterest API - 在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。 – 2013-05-24 07:14:13
我讓另一位用戶在不同的地區測試了他們的EC2實例,並且也被拒絕了。我的結論是,Pinterest阻止了AWS IP地址。 – Abundnce10 2013-05-26 17:23:18