[英]Access LinkedIn Profile with Python
我正在尝试通过 API 以计算方式访问我自己的 LinkedIn 个人资料以下载我自己的帖子。 最近有三个 Python 包装器可以访问我的个人资料,例如linkedin-sdk 、 pawl 、 LinkedIn V2 。 但是,我一直无法让它们工作。 问题是身份验证。 我见过著名的LinkedIn-API wrapper ,但它的身份验证过程复杂且困难,可能是由于 LinkedIn 更改了其身份验证过程和访问范围。
根据去年的这个教程,我已经能够访问我自己的个人资料来查看我的姓名、国家、语言和 ID。
import requests
#get access_token by post with user & password
#Step 1 - GET to request for authentication
def get_auth_link():
URL = "https://www.linkedin.com/oauth/v2/authorization"
client_id= 'XXXX'
redirect_uri = 'http://localhost:8080/login'
scope='r_liteprofile'
PARAMS = {'response_type':'code', 'client_id':client_id, 'redirect_uri':redirect_uri, 'scope':scope}
r = requests.get(url = URL, params = PARAMS)
return_url = r.url
print('Please copy the URL and paste it in browser for getting authentication code')
print('')
print(return_url)
get_auth_link()
# Make a POST request to exchange the Authorization Code for an Access Token
import json
def get_access_token():
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'OAuth gem v0.4.4'}
AUTH_CODE = 'XXXX'
ACCESS_TOKEN_URL = 'https://www.linkedin.com/oauth/v2/accessToken'
client_id= 'XXXX'
client_secret= 'XXXX'
redirect_uri = 'http://localhost:8080/login'
PARAM = {'grant_type': 'authorization_code',
'code': AUTH_CODE,
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': client_secret}
response = requests.post(ACCESS_TOKEN_URL, data=PARAM, headers=headers, timeout=600)
data = response.json()
print(data)
access_token = data['access_token']
return access_token
get_access_token()
access_token = 'XXXX'
def get_profile(access_token):
URL = "https://api.linkedin.com/v2/me"
headers = {'Content-Type': 'application/x-www-form-urlencoded','Authorization':'Bearer {}'.format(access_token),'X-Restli-Protocol-Version':'2.0.0'}
response = requests.get(url=URL, headers=headers)
print(response.json())
get_profile(access_token)
一旦我将范围从r_liteprofile
更改为r_basicprofile
我就会得到一个未授权的_scope_error:r_basicprofile 未授权您的应用程序。 在我的开发者页面中,我有r_emailaddress
、 r_liteprofile
和w_member_social
授权范围。 但只有r_liteprofile
有效。 据我从LinkedIn文档中了解到,评论无法下载?
所以真正的大问题是,评论可以通过 API 下载吗?
机器人或爬虫不是一种选择,因为它们需要 LinkedIn 的明确许可,而我没有。
更新:所以请不要非法解决方案。 我在写这篇文章之前就知道它们存在。
谢谢你的帮助!
我发现使用 tomquirk 的linkedin-api登录非常简单。 但是,当帖子没有任何评论时会引发 KeyError。 我将它固定在一个叉子中,然后提交了一个拉取请求。 如果您使用python setup.py install
安装 fork,以下代码将获取您所有带有评论的帖子:
from linkedin_api import Linkedin
import getpass
print("Please enter your LinkedIn credentials first (2FA must be disabled)")
username = input("user: ")
password = getpass.getpass('password: ')
api = Linkedin(username, password)
my_public_id = api.get_user_profile()['miniProfile']['publicIdentifier']
my_posts = api.get_profile_posts(public_id=my_public_id)
for post in my_posts:
post_urn = post['socialDetail']['urn'].rsplit(':', 1)[1]
print('POST:' + post_urn + '\n')
comments = api.get_post_comments(post_urn, comment_count=100)
for comment in comments:
commenter = comment['commenter']['com.linkedin.voyager.feed.MemberActor']['miniProfile']
print(f"\t{commenter['firstName']} {commenter['lastName']}: {comment['comment']['values'][0]['value']}\n")
注意:这里不使用官方API,根据README.md:
该项目违反了 Linkedin 的用户协议第 8.2 条,因此,Linkedin 可能(并且将)暂时或永久禁止您的帐户。
但是,只要您仅从自己的帐户中抓取评论,就可以了。
下载不违反 LinkedIn 条款和条件的评论有两种合法选择。 两者都需要领英的许可。
选项 A: 评论 API
评论 API是页面管理 API 的一部分,而页面管理 API 又是营销开发人员计划 (MDP) 的一部分。 LinkedIn 在此处描述了其营销开发人员计划的申请流程。 它需要填写一个指定用例的表格。 然后 LinkedIn 决定是否授予访问权限。 这些用例将受到限制或不被批准。
选项 B: Web 爬取和抓取 LinkedIn 的豁免(白名单)
此处描述了豁免过程。
我选择选项 A。让我们看看他们是否允许我访问。 我会相应地更新帖子。
2022 年 19 月 5 日更新
LinkedIn 已授予 MDP 的权限。 大约花了2周时间。
2022 年 5 月 27 日更新
这是获取个人帖子的绝佳教程。 获取公司页面帖子是另一回事- 完全- 所以打开了一个新查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.