[英]How to get data (items) from eBay Finding API through Google App Engine?
我試圖通過 Google App Engine 為一個項目從 eBay 查找 API 中檢索數據。 這似乎與 Google App Engine 阻止常規請求有關。
我嘗試使用urlfetch
和urllib3
但無濟於事。 我正在嘗試以 JSON 格式檢索項目數據。
這是我的第一次嘗試:
def get(self):
requests_toolbelt.adapters.appengine.monkeypatch()
http = urllib3.PoolManager()
key = 'WailSahb-Analysis-PRD-4c970d9ce-c9a80d1e'
search_term = 'laptop'
url = ('http://svcs.ebay.com/services/search/FindingService/v1\
?OPERATION-NAME=findItemsByKeywords\
&sortOrder=PricePlusShippingLowest\
&buyerPostalCode=92128&SERVICE-VERSION=1.13.0\
&SECURITY-APPNAME=' + key +
'&RESPONSE-DATA-FORMAT=JSON\
&REST-PAYLOAD\
&itemFilter(0).name=Condition\
&itemFilter(0).value=New\
&itemFilter(1).paramName=Currency\
&itemFilter(1).paramValue=EUR\
&itemFilter(2).paramName=FeedbackScoreMin\
&itemFilter(2).paramValue=10\
&paginationIntput.entriesPerPage=100\
&outputSelector(0)=SellerInfo\
&descriptionSearch=FALSE\
&paginationIntput.pageNumber=1\
&keywords=' + search_term)
url = url.replace(" ", "%20")
result = http.request('GET', url)
self.response.write(result)
使用這種方法,我收到以下錯誤:
MaxRetryError: HTTPSConnectionPool(host='pages.ebay.com', port=443): 最大重試次數超過 url: /messages/page_not_found.html?eBayErrorEventName=p4buoajkbnmbehq%60%3C%3Dosu71%2872%3A4505-2016.158.08 28.47.151.MST(由協議錯誤引起('連接中止。',錯誤(13,'權限被拒絕')))
我也嘗試過這種方法:
def get(self):
try:
api = Connection(appid='WailSahb-Analysis-PRD-4c970d9ce-c9a80d1e', config_file=None)
response = api.execute('findItemsAdvanced', {'keywords': 'legos'})
assert(response.reply.ack == 'Success')
assert(type(response.reply.timestamp) == datetime.datetime)
assert(type(response.reply.searchResult.item) == list)
item = response.reply.searchResult.item[0]
assert(type(item.listingInfo.endTime) == datetime.datetime)
assert(type(response.dict()) == dict)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(result.data)
except ConnectionError as e:
self.response.write(e.response.dict())
我收到此錯誤:
TypeError: super(type, obj): obj must be an instance or subtype of type
任何人都可以幫我解決這個問題。
感謝您的時間。
我試圖重現您的問題並不得不更改一些內容,但最終能夠成功獲取您嘗試請求的頁面。
我首先逐字復制了你的第一次嘗試,我得到的錯誤略有不同:
MaxRetryError: HTTPSConnectionPool(host='pages.ebay.com', port=443): Max retries exceeded with url: /messages/page_not_found.html?eBayErrorEventName=p4buoajkbnmbehq%60%3C%3Dsm%7E71%287147606-2018.08.23.14.59.22.829.MST (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",))
這向我表明問題是缺少或無效的 SSL 模塊。 您沒有分享您的app.yaml
,但我必須添加以下內容才能使 HTTPS 請求成功:
libraries:
- name: ssl
version: latest
然而,最終的應用引擎響應是不正確的,因為result
是一個urllib3.response.HTTPResponse
對象,而不是一個實際的響應。
為了解決這個問題,我改變了這一行:
self.response.write(result)
到
self.response.write(result.content)
然后這按預期工作。
以下是對我有用的文件的最終版本:
在app.yaml
:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
libraries:
- name: ssl
version: latest
在main.py
:
import webapp2
from requests_toolbelt.adapters import appengine
import urllib3
class MainPage(webapp2.RequestHandler):
def get(self):
appengine.monkeypatch()
http = urllib3.PoolManager()
key = 'WailSahb-Analysis-PRD-4c970d9ce-c9a80d1e'
search_term = 'laptop'
url = (
'http://svcs.ebay.com/services/search/FindingService/v1\
?OPERATION-NAME=findItemsByKeywords\
&sortOrder=PricePlusShippingLowest\
&buyerPostalCode=92128&SERVICE-VERSION=1.13.0\
&SECURITY-APPNAME=' + key +
'&RESPONSE-DATA-FORMAT=JSON\
&REST-PAYLOAD\
&itemFilter(0).name=Condition\
&itemFilter(0).value=New\
&itemFilter(1).paramName=Currency\
&itemFilter(1).paramValue=EUR\
&itemFilter(2).paramName=FeedbackScoreMin\
&itemFilter(2).paramValue=10\
&paginationIntput.entriesPerPage=100\
&outputSelector(0)=SellerInfo\
&descriptionSearch=FALSE\
&paginationIntput.pageNumber=1\
&keywords=' + search_term)
url = url.replace(" ", "%20")
result = http.request('GET', url)
self.response.write(result.data)
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.