-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.py
49 lines (40 loc) · 1.92 KB
/
twitter.py
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
from playwright.sync_api import sync_playwright
def scrape_profile(url: str) -> dict:
"""
Scrape a Twitter profile page for profile data.
Return the profile data as a dictionary.
"""
with sync_playwright() as pw:
browser = pw.chromium.launch()
context = browser.new_context(viewport={"width": 1920, "height": 1080})
page = context.new_page()
page.goto(url)
page.wait_for_selector("[data-testid='UserDescription']")
# Extract profile data
profile_data = {
"name": page.inner_text("[data-testid='UserProfileHeader_Items'] > span"),
"handle": page.inner_text("[data-testid='UserProfileHeader_Items'] > div"),
"description": page.inner_text("[data-testid='UserDescription']"),
"location": page.inner_text("[data-testid='UserProfileHeader_Items'] > span:nth-child(3)"),
"website": page.inner_text("[data-testid='UserProfileHeader_Items'] > span:nth-child(4) a"),
"join_date": page.inner_text("[data-testid='UserProfileHeader_Items'] > span:nth-child(6)"),
"follower_count": page.inner_text("[data-testid='UserProfileHeader_Items'] > div:nth-child(3) span"),
"following_count": page.inner_text("[data-testid='UserProfileHeader_Items'] > div:nth-child(4) span"),
"tweet_count": page.inner_text("[data-testid='UserProfileHeader_Items'] > div:nth-child(5) span"),
}
browser.close()
return profile_data
if __name__ == "__main__":
profile_urls = [
"https://twitter.com/Scrapfly_dev",
"https://twitter.com/OpenAI",
"https://twitter.com/Twitter",
]
for url in profile_urls:
try:
profile_data = scrape_profile(url)
print("Profile Data:")
print(profile_data)
print("-----")
except Exception as e:
print(f"An error occurred while scraping {url}: {str(e)}")