我用Flask写了一个网站;它会为手机和桌面返回不同的结果。我在服务器上使用request.user_agent.platform
来检查平台。烧瓶请求似乎没有Google-Bot
这里是我的代码为Android,台式电脑和其他移动设备返回不同的网站:
if request.user_agent.platform == "android":
ismobile = True
# do something when the user is on android
for pf in ["iphone", "iPad", "ipad", "Windows Phone OS", "windows phone os", "BlackBerry", "windows phone", "Windows Phone", "WindowsPhone", "android"]:
if pf in request.user_agent.platform:
ismobile = True
# do something when the user is on any other mobile device
if not ismobile:
# do something when the user is on desktop
到目前为止,此代码的工作。但是当谷歌博特发送一个请求,一个ValueError被抛出:
TypeError
TypeError: argument of type 'NoneType' is not iterable
(与谷歌标记工具测试,这里的full stack trace)。
当Google访问我的页面时,似乎有些东西是无。有谁知道我可以如何防止我的应用程序抛出错误?
编辑:我忘了:当我访问请求时会抛出错误。像if request is None:
这样的代码仍会产生错误。
为什么不测试'是None'呢? –
@MartijnPieters我试过,但错误仍然抛出。 – palsch
@Functino谢谢,编辑是完美的。 – palsch