

纯代码模拟登陆 Instagram获取图片和视频
source link: https://shingle.me/post/%E7%BA%AF%E4%BB%A3%E7%A0%81%E6%A8%A1%E6%8B%9F%E7%99%BB%E9%99%86-instagram%E8%8E%B7%E5%8F%96%E5%9B%BE%E7%89%87%E5%92%8C%E8%A7%86%E9%A2%91/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

纯代码模拟登陆 Instagram获取图片和视频
抓个请求,试试错, easy
首先祭出参考链接:https://stackoverflow.com/questions/49265339, 站在前人的肩膀上完成的任务
爬取指定帖子
如果你想爬取某一个指定帖子中的图片或视频, 很简单!而且目前还是失效,直接在 url 后面加?__a=1
即可获取到 json 数据
https://www.instagram.com/p/BejRyk4njXV/ 这是原链接
https://www.instagram.com/p/BejRyk4njXV/?__a=1 这是 json 数据
爬取某个用户的所有 ins 信息
这个算是本文重点吧,其实搞清楚 ins 那边需要什么就可以搞定了.
-
首先访问一次该用户的主页,例如
https://www.instagram.com/9gag/
直接扒网页源码获取到 userid, 正则
"profilePage_\d+"
匹配就能获取到 id为259220806
,此时还需要记一下csrftoken
这个 cookie 值 -
由于调用用户信息的接口是需要登陆的,所以第二步就是模拟登陆 ins 了
要请求的url 为
https://www.instagram.com/accounts/login/ajax/
需要 post 三个参数
username
:你的用户名password
: 你的密码(明文~~震惊)queryParams
:{}
以上可以通过浏览器抓包网络请求看到,如果以后更新了方法大同小异
接着传入几个关键的 header 信息
User-Agent
: 填浏览器 ua 即可Referer
:https://www.instagram.com/
x-csrftoken
: 第一步获取到的值Content-Type
:application/x-www-form-urlencoded
这一个可有可无登陆成功后记录下来到返回的 key 为
sessionid
的 Cookie 值 -
最后一步就很简单了,带着 第二步获取到的Cookie 值访问 api 即可
获取用户 ins 的 api 为
https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={"id"259220806:,"first":10,"after":"XXXXXXXX"}
query_hash
: 用上面那一串就行,以后不能用在研究id
替换成你想要爬取的人的 idfirst
是需要返回几条数据after
是从什么地方开始返回, 用于获取下一页的情况,传 json 中的end_cursor
即可获取下一页的数据返回的 json 中,主要数据在
edges
节点下面, 遍历即可
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK