我试图通过 Google App Engine 从 eBay Find 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): Max retries exceeded with url: /messages/page_not_found.html?eBayErrorEventName=p4buoajkbnmbehq%60%3C%3Dosu71%2872%3A4505-2018.08.16.15.28.47.151.MST (Caused by ProtocolError('Connection aborted.', error(13, 'Permission denied')))
我也尝试过这种方法:
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)
关于python - 如何通过 Google App Engine 从 eBay Finding API 获取数据(项目)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886180/