应用程序引擎 urlfetch 问题
我尝试使用应用程序引擎中的 urlfetch 请求 https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=[http://www.my-website.dk/]&key=[my-key]但它不起作用。
当我访问它并将 my-url 硬编码到请求中时,如下所示: https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.my-website.dk/&key=[ my-key] 它工作正常,但是当我使用 urlfetch.fetch("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=%s&key=[my-key]", “http://www.my-website.dk”)它不起作用,我也尝试过:
page_content = urlfetch.fetch(
url="https://www.googleapis.com/pagespeedonline/v1/runPagespeed",
payload=params,
method=urlfetch.GET
)
然后像这样在有效负载中提供参数:
params = urllib.urlencode({
"url": page.link,
"key": "[my-key]"
})
但结果是相同的,它不起作用并且服务给我 HTTP状态代码 400。我还尝试添加 urlfetch.fetch(u("http://...", page.link) 但结果是相同的。
我根据 systempuntoout 的回复编辑了代码,以防任何人都应该运行陷入同样的问题:
params = urllib.urlencode({
"url": page.link,
"key": "AIzaSyAFpm6W_OmjQl33JC98mAPkvrdGmrR0i4Y"
})
page_content = urlfetch.fetch("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?%s" % params)
Im trying to request https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=[http://www.my-website.dk/]&key=[my-key] using urlfetch from app engine but it's not working.
When I access it and hardcode my-url into the request like this: https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.my-website.dk/&key=[my-key] it's working fine, but when I use urlfetch.fetch("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=%s&key=[my-key]", "http://www.my-website.dk") it's not working, I have also tried:
page_content = urlfetch.fetch(
url="https://www.googleapis.com/pagespeedonline/v1/runPagespeed",
payload=params,
method=urlfetch.GET
)
and then serving the parameters in the payload like this:
params = urllib.urlencode({
"url": page.link,
"key": "[my-key]"
})
but the result is the same, it's not working and the service gives me HTTP status code 400. I also tried adding urlfetch.fetch(u("http://...", page.link) but the result is the same.
I edited the code based on the reply from systempuntoout incase any one should run into the same problem:
params = urllib.urlencode({
"url": page.link,
"key": "AIzaSyAFpm6W_OmjQl33JC98mAPkvrdGmrR0i4Y"
})
page_content = urlfetch.fetch("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?%s" % params)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
urlfetch
调用出现错误,因为您向函数传递了两个参数。您应该在两个字符串之间使用
%
来仅向函数传递一个 url 参数。那么,您是否尝试过 urlencode 第二个网址?
First, the
urlfetch
call has an error because you are passing two parameters to the function.You should use the
%
between the two strings to pass just one url parameter to the function.Then, have you tried to urlencode the second url?
有效负载仅适用于 POST/PUT 请求。对于 GET,您的参数必须是 URL 的一部分
payload is only applicable for POST/PUT requests. For GET, your params need to be part of the URL