一个用于从Instagram个人资料中抓取地理标记位置的工具。输出为JSON和交互式地图。
sudo apt install chromium-chromedriver && chmod a+x /usr/bin/chromedriver
git clone https://github.com/bernsteining/instaloctrack
cd instaloctrack
pip3 install .
或使用Docker:
sudo docker build -t instaloctrack -f Dockerfile .
instaloctrack -h
usage: instaloctrack [-h] [-t TARGET_ACCOUNT] [-l LOGIN] [-p PASSWORD] [-v]
Instagram location data gathering tool. Usage: python3 instaloctrack.py -t <target_account>
optional arguments:
-h, --help show this help message and exit
-t TARGET_ACCOUNT, --target TARGET_ACCOUNT
Instagram profile to investigate
-l LOGIN, --login LOGIN
Instagram profile to connect to, in order to access
the instagram posts of the target account
-p PASSWORD, --password PASSWORD
Password of the Instagram profile to connect to
-v, --visual Spawns Chromium GUI, otherwise Chromium is headless
例如:
instaloctrack -t <target_account>
如果目标账户是私密的,并且你有一个关注了目标账户的账号,你可以通过登录会话抓取数据:
instaloctrack -t <target_account> -l <your_account> -p <your_password>
或使用Docker:
sudo docker run -v /tmp/output:/tmp/output instaloctrack -t <target_account> -o /tmp/output
首先,我们通过滚动整个Instagram个人资料,利用selenium的webdriver检索该账户的所有图片链接。
然后,我们异步(asyncio)处理每个图片链接,检查图片描述中是否包含位置信息,若存在则提取位置数据和时间戳。
由于Instagram不提供GPS坐标,我们只能获得地点名称,因此需要进行地理编码(即从地点名称获取GPS坐标)。
为此,我使用了Nominatim的优秀API,它基于OpenStreetMap。对于我们的用途,不需要API密钥,并且我们每秒请求一次GPS坐标,以遵守Nominatim的使用政策。
最终,当获取到所有GPS坐标后,我们生成一个嵌入JavaScript的HTML文件(借助jinja2模板),利用Leaflet库在OpenStreetMap上标记所有位置。同样,这一步也无需API密钥。
此外,脚本收集的数据(位置名称、时间戳、GPS坐标、错误)会转储到一个JSON文件中,以便重复使用。
以下是对法国前总统Instagram账户@fhollande的输出示例:

热力图:

点击标记时可查看的信息:

关于位置数据的统计:

JSON数据转储(仅截取部分以展示某一位置的格式):
{
"link": "https://www.instagram.com/p/-Q_9EvR9eu",
"place": {
"id": "290297",
"name": "Musée du quai Branly - Jacques Chirac",
"slug": "musee-du-quai-branly-jacques-chirac",
"street_address": " 37 quai Branly",
" zip_code": " 75007",
" city_name": " Paris",
" region_name": " ",
" country_code": " FR"
},
"timestamp": "2015-11-19",
"gps": {
"lat": "48.8566969",
"lon": "2.3514616"
}
}