【问题标题】:Using python to collect user information from Instagram使用python从Instagram收集用户信息
【发布时间】:2019-10-24 19:49:14
【问题描述】:

我目前正在使用 python 从 instagram 上的用户那里收集信息,使用包含 instagram 用户链接的文本文件。虽然我可以收集关注者的数量、关注的数量和帖子的数量,但我希望能够从用户那里收集生物信息。收集生物信息将使我最终能够解析这些信息并收集电子邮件。我能做到这一点的最好和最简单的方法是什么?

我在 Python 方面没有经验,所以我从互联网上获取了一个示例代码。我试图分析代码并使用我所知道的来修改它以满足我的需要,但没有结果。

import requests
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json


class Insta_Info_Scraper:

    def getinfo(self, url):
        html = urllib.request.urlopen(url, context=self.ctx).read()
        soup = BeautifulSoup(html, 'html.parser')
        data = soup.find_all('meta', attrs= {'property':'og:description'})
        text = data[0].get('content').split()
        user = '%s %s %s' % (text[-3], text[-2], text[-1])
        followers = text[0]
        following = text[2]
        posts = text[4]
        email = ""
        print ('User:', user)
        print ('Followers:', followers)
        print ('Following:', following)
        print ('Posts:', posts)
        print ('Email:', email)
        print ('---------------------------')

    def main(self):
        self.ctx = ssl.create_default_context()
        self.ctx.check_hostname = False
        self.ctx.verify_mode = ssl.CERT_NONE

        with open('users.txt') as f:
            self.content = f.readlines()
        self.content = [x.strip() for x in self.content]
        for url in self.content:
            self.getinfo(url)


if __name__ == '__main__':
    obj = Insta_Info_Scraper()
    obj.main()

目前,我将一个空字符串作为“电子邮件”变量的值,但最终希望将其替换为可以从特定用户那里获取电子邮件的代码。

【问题讨论】:

  • 你好@rivas142。很高兴您包含并格式化了代码。您还应该在此处查看有关格式化的更多信息:format your code
  • 它的工作,但不是所有的帖子都只能得到最后12个帖子,那么如何获得所有的帖子

标签: python beautifulsoup instagram screen-scraping


【解决方案1】:

访问 Instagram 的公共数据结构的便捷工具是 Instaloader,这是一个 Python 包,它提供 Python 模块和 CLI 来访问 Instagram。完成pip install instaloader 安装后,您可以轻松获取保存在 JSON 文件中的 Profile 元数据

instaloader --no-posts --no-profile-pic --no-compress-json profile1 [profile2 ...]

然后您可以使用jq,“轻量级且灵活的命令行 JSON 处理器”,提取刚刚保存的信息,例如以下命令打印 profile1 的传记:

jq -r .node.biography profile1/profile1_*.json

同样,一种不让 Python 访问相同信息的方法:

import instaloader
L = instaloader.Instaloader()
profile = instaloader.Profile.from_username(L.context, 'profile1')
print(profile.biography)

【讨论】:

    【解决方案2】:

    最好的方法是使用第三方库,例如instagram_private_api

    示例:

    from instagram_web_api import Client
    
    web_api = Client(auto_patch=True, drop_incompat_keys=False)
    user_info = web_api.user_info2('instagram')
    print(user_info)
    

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2014-04-07
      • 2017-09-22
      • 2014-03-30
      相关资源
      最近更新 更多