0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

flaskでuser_agentのbrowserが取れなかった話

Posted at

久々の投稿。
最近、生成AIのプロジェクトに関わることが増えてきたので、とにかくPythonをかけるように勉強中です。

Head First Pythonを読んでいるのですが、その中で、browserを取る下記のコードが書かれてた。

res.user_agent.browser

しかし、上記は「None」となってしまう。

dir(res.user_agent)

とすると、browserはあるので、取れるはずだが、、正直理由はわからない。

根本原因は分からなかったので、他の方法がないかを探したところ下記で動作する。

from ua_parser import user_agent_parser

parsed_string = user_agent_parser.Parse(str(req.user_agent))
browser = parsed_string["user_agent"]["family"]

これで、browserに「Chrome」や「Safari」が入ることになる。
試されたい。

参考

uap-python
Can't get user browser with flask request

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?