{"id":18881078,"url":"https://github.com/tylertemp/baidutongji","last_synced_at":"2026-02-20T23:30:19.378Z","repository":{"id":86362573,"uuid":"47549060","full_name":"TylerTemp/baidutongji","owner":"TylerTemp","description":"auto login and get data from tongji.baidu.com","archived":false,"fork":false,"pushed_at":"2016-01-07T14:18:10.000Z","size":16,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-31T03:26:17.157Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TylerTemp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-07T11:50:02.000Z","updated_at":"2018-03-13T12:34:21.000Z","dependencies_parsed_at":"2023-03-17T07:45:44.063Z","dependency_job_id":null,"html_url":"https://github.com/TylerTemp/baidutongji","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerTemp%2Fbaidutongji","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerTemp%2Fbaidutongji/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerTemp%2Fbaidutongji/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TylerTemp%2Fbaidutongji/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TylerTemp","download_url":"https://codeload.github.com/TylerTemp/baidutongji/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239850449,"owners_count":19707348,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T06:47:15.712Z","updated_at":"2026-02-20T23:30:19.315Z","avatar_url":"https://github.com/TylerTemp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"baidutongji\n============\n\nA utilities to login to \u003chttp://tongji.baidu.com\u003e and grab the first\nprevew page data and location data.\n\n\nInstall\n-------\n\n```bash\npip install git+git://github.com/TylerTemp/baidutongji.git\n```\n\nUsage\n---------\n\n### Initialize ###\n\n```python\nfrom baidutongji import BaiduTongji\n```\n\n```\nBaiduTongji(username, password, site_id, save_image_file='/TMPDIR/validate.png', save_code_file='/TMPDIR/validate')\n```\n\n*   `username`: (required) Baidu Tongji username\n*   `password`: (required) Baidu Tongji password\n*   `site_id`: (required) You site id on Baidu Tongji\n*   `save_image_file`: (optional) The path to save the validate image. Use `validate.png` under temp dir by default.\n*   `save_code_file`: (optional) The path where `tesseract` save the ORC result, by default it's in `validate`\n    (`tesseract` will add `.txt` suffix) under temp dir\n\n### Login ###\n\n```python\nfrom baidutongji import BaiduTongji\nl = BaiduTongji(...)\nl.pre_login(sleep=1)\n```\n\n`.pre_login` will get some cookie and save the validate image. The file path is `save_image_file`\n`sleep` can set the sleep time to aviod `RESTful` API explode.\n\n\n```\nl.do_login(code)\n```\n\n`.do_login` to login the page. `code` is the validate code.\nReturn `True` for login success, `False` otherwise.\n\n### Auto Login ###\n\nIf you've already install `tesseract` on your computer, you can use `.login`\nto auto login.\n\n```python\nfrom baidutongji import BaiduTongji\nl = BaiduTongji(...)\nwhile not l.login():    # ORC \u0026 try login\n    print('retry...')    # failed, re-try\n```\n\n### siteid\n\nYou site has one or more site ids.\n\nBaiduTongji will set the default site id as your site on Baidu.\n\nYou change it by\n\n``` python\n\u003e\u003e\u003e l.site_ids\n[{'default': True, 'id': '7479735', 'url': 'example.com'},\n {'default': False, 'id': '7746580', 'url': 'm.example.com'}]\n\u003e\u003e\u003e l.site_id = '7746580'\n```\n\n### Get Preview ###\n\n`l.get_perview()` return the first page of Baidu Tongji data.\n\nThe format looks like:\n\n```\n{'everyday_avg': {'avg_visit_time': 593,\n                  'bounce_ratio': 20.05,\n                  'ip_count': 1063,\n                  'pv_count': 54928,\n                  'trans_count': None,\n                  'visitor_count': 2191},\n 'forecase_today': {'avg_visit_time': {'flag': 0, 'val': None},\n                    'bounce_ratio': {'flag': 0, 'val': None},\n                    'ip_count': {'flag': -1, 'val': 446},\n                    'pv_count': {'flag': -1, 'val': 15889},\n                    'trans_count': {'flag': 0, 'val': None},\n                    'visitor_count': {'flag': -1, 'val': 783}},\n 'today': {'avg_visit_time': 497,\n           'bounce_ratio': 20.66,\n           'ip_count': 442,\n           'pv_count': 15671,\n           'trans_count': None,\n           'visitor_count': 741},\n 'yesterday': {'avg_visit_time': 450,\n               'bounce_ratio': 20.33,\n               'ip_count': 750,\n               'pv_count': 35577,\n               'trans_count': None,\n               'visitor_count': 1727},\n 'yesterday_now': {'avg_visit_time': 467,\n                   'bounce_ratio': 20.56,\n                   'ip_count': 730,\n                   'pv_count': 34849,\n                   'trans_count': None,\n                   'visitor_count': 1616}},\n 'history_peak': {'avg_visit_time': {'date': datetime.datetime(2015, 12, 28, 0, 0),\n    'show': 0,\n    'val': 907},\n   'bounce_ratio': {'date': datetime.datetime(2015, 12, 22, 0, 0),\n    'show': 0,\n    'val': 28.07},\n   'ip_count': {'date': datetime.datetime(2015, 12, 29, 0, 0),\n    'show': 1,\n    'val': 736},\n   'pv_count': {'date': datetime.datetime(2015, 12, 29, 0, 0),\n    'show': 1,\n    'val': 29717},\n   'trans_count': None,\n   'visitor_count': {'date': datetime.datetime(2015, 12, 29, 0, 0),\n    'show': 1,\n    'val': 994}}\n```\n\n### Get Location ###\n\n`get_region(date)` get one day information of all provinces.\n`date` can be `datetime.date` object\n(which , must has `year`, `month`, `day` attribute)\n\n```python\nimport datetime\ndatetime.date(year=2015, month=12, day=2)\nresult = l.get_region(d)\nprint(result)\n```\n\n`has_city_regions()` return all provinces which contains sub-cities.\n\n`get_city(region, date)` return the information of cities under `region` province\nat `date` date.\n\n```python\nfor each in l.has_city_regions():\n    this_result = l.get_city(each, d)\n    print(this_result)\n```\n\n### Logout ###\n\n`.logout()` will delete all cookie to start a new session.\n\nError\n-----\n\nraise `baidutongji.NotLoginError` when you try to get data with a outdated\nsession or not logged in.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylertemp%2Fbaidutongji","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftylertemp%2Fbaidutongji","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftylertemp%2Fbaidutongji/lists"}