Spaces:
Sleeping
Sleeping
File size: 1,289 Bytes
5318b09 |
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 |
from apify_client import ApifyClient
from dotenv import load_dotenv
import os
import json
# Load environment variables
load_dotenv()
# Get API token
api_token = os.getenv("APIFY_API_TOKEN")
# Initialize client once (global)
client = ApifyClient(api_token)
def scrape_linkedin_profile(profile_url: str) -> dict:
"""
π Scrapes a LinkedIn profile using Apify and returns the data as a Python dict.
"""
try:
run_input = {"profileUrls": [profile_url]}
run = client.actor("dev_fusion/Linkedin-Profile-Scraper").call(run_input=run_input)
items = list(client.dataset(run["defaultDatasetId"]).iterate_items())
if items:
with open("scraped_profile.json", "w") as f:
json.dump(items[0], f, indent=2)
return items[0]
else:
print("β οΈ No data found in dataset.")
return {}
except Exception as e:
print(f"β Error during scraping: {e}")
return {}
# π§ͺ OPTIONAL: test code only runs when this file is executed directly
if __name__ == "__main__":
test_url = "https://www.linkedin.com/in/sri-vallabh-tammireddy/"
profile = scrape_linkedin_profile(test_url)
print(json.dumps(profile, indent=2))
|