/ins_crawler

爬取Instagram上某一账户的全部post,包括正文,点赞数,评论数,发帖时间,帖子链接,并且下载相应的图片或视频

Primary LanguagePythonMIT LicenseMIT

ins-cralwler - Instagram爬虫

License python version

forthebadge forthebadge forthebadge

爬取全部帖子

  • 正文
  • 点赞数
  • 评论数
  • 发帖时间
  • 帖子链接
  • 正文图片
  • 缩略图
  • 评论
  • 视频
  • 正在关注



准备食用餐具

克隆

git clone https://github.com/trebleC/ins_crawler.git

安装依赖

pip install -r requirements.txt



食用方法

  • 修改用户昵称
  • 在instagram的用户主页按下F12,获取对应的query_hash以及cookie
user_name = 'everyone.is.storyteller'

query_hash=' '

headers   = { 
                "cookie": " "
            }
  • 数据库连接默认,如需修改save_mongo(dict)中的值
def save_mongo(dict):
    conn = MongoClient('localhost', 27017) #地址,端口号
    db = conn.spider    #数据库
    my_set = db.ins   #集合



字典

edge = js_data["entry_data"]["ProfilePage"][0]["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"]

edge["node"]["taken_at_timestamp"] #时间戳
edge["node"]["display_url"] #图片地址
edge["node"]["edge_media_to_caption"]["edges"][0]["node"]["text"] #正文内容
edge["node"]["shortcode"] #shortcode
edge["node"]["edge_media_to_comment"]["count"] #评论数
edge["node"]["edge_media_preview_like"]["count"] #点赞数
edge["node"]["thumbnail_resources"][-1]["src"] #缩略图地址

数据结构 | debug_content.json



截图功能

获取已经存入mongodb中的每个帖子的url,screenshot.py实现了对每篇帖子截图的功能。

  • 截图功能

数据存储结构

在MongoDB中,每个帖子以一条信息的方式存储,每条信息的具体字段意义如下表所示:

key 意义
_id 帖子的时间戳
timestamp 帖子的时间戳
shortcode 帖子的标识符,对于每个用户的所有帖子来说是唯一的
imgs_url 图片地址
content 帖子正文
counts_comment 评论数
counts_like 点赞数
thumbnail_url 缩略图地址


对于只用过Mysql同学,下面是MongoDB安装

Windows

下载
https://www.mongodb.com/download-center#community

按步骤安装后启动 

命令行
C:\Users\as> mongod

Linux

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz    # 下载
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz                                   # 解压

mv  mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb                         # 将解压包拷贝到指定目录

参考

菜鸟教程 | https://www.runoob.com/mongodb/mongodb-window-install.html

License

The code is available under the MIT License.