我目前正在使用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()
目前,我为' email‘变量设置了一个空字符串作为值,但希望最终将其替换为将从特定用户获取电子邮件的代码。
发布于 2019-07-09 00:19:49
访问Instagram公共数据结构的一个方便的工具是Instaloader,这是一个Python包,提供了Python模块和命令行界面来访问Instagram。在执行pip install instaloader
来安装它之后,您可以使用以下命令轻松地获得保存在JSON文件中的概要文件的元数据
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)
发布于 2019-06-10 21:42:29
最好的方法是使用像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)
https://stackoverflow.com/questions/56525911
复制相似问题