-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprofile.py
More file actions
63 lines (50 loc) · 2.54 KB
/
Copy pathprofile.py
File metadata and controls
63 lines (50 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
LinkedIn Profile - Chocodata LinkedIn Scraper API
Public member profile by username: name, headline, location, about, current company.
Note: LinkedIn does not render job titles, date ranges or degrees on the logged-out
profile page, so `experience[].title`, `experience[].date_range` and
`education[].degree` come back null. We return the public surface verbatim.
pip install requests
export CHOCODATA_API_KEY="your_key" # free: 1,000 requests, one-time
python linkedin_scraper_api_codes/profile.py williamhgates
Docs: https://chocodata.com/docs
"""
import json
import os
import sys
import requests
API = "https://api.chocodata.com/api/v1/linkedin/profile"
KEY = os.environ.get("CHOCODATA_API_KEY")
if not KEY:
sys.exit("Set CHOCODATA_API_KEY first. Free key (1,000 requests, one-time): https://chocodata.com")
def _check(r) -> None:
"""Map the API's documented errors onto actionable messages instead of a traceback."""
if r.status_code == 400:
sys.exit(f"400 invalid_params: {r.text[:200]}")
if r.status_code == 401:
sys.exit("401 INVALID_API_KEY: key missing or not recognised. Get one: https://chocodata.com")
if r.status_code == 402:
sys.exit("402 INSUFFICIENT_CREDITS: balance exhausted. Top up or upgrade: https://chocodata.com/pricing")
if r.status_code == 429:
sys.exit("429 RATE_LIMITED: over your plan's concurrency. Back off and retry.")
if r.status_code == 404:
sys.exit(f"404 item_not_found: {r.json().get('message', 'does not exist')} (not retryable, not charged)")
if r.status_code == 502:
sys.exit("502 target_unreachable: LinkedIn refused every attempt for this request. Retryable, and you were not charged.")
r.raise_for_status()
def profile(username: str) -> dict:
"""Fetch a public LinkedIn profile by username (linkedin.com/in/<username>)."""
r = requests.get(API, params={"api_key": KEY, "username": username}, timeout=90)
_check(r)
return r.json()
if __name__ == "__main__":
user = sys.argv[1] if len(sys.argv) > 1 else "williamhgates"
data = profile(user)
print(json.dumps(data, indent=2)[:1200])
print()
print(f"{data['name']} | {data['headline']} | {data['location']}")
print(f"{data['followers']:,} followers | now at: {data['current_company']}")
# The nulls are real. Code against them.
companies = [e["company"] for e in data.get("experience", []) if e.get("company")]
print(f"experience: {len(companies)} companies ({', '.join(companies)}) "
f"- titles/dates are null on the public page")