scrape tweet analytics and user avatar data
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from selenium.webdriver import Chrome
|
from selenium.webdriver import Chrome
|
||||||
from selenium.common.exceptions import NoSuchElementException
|
from selenium.common.exceptions import NoSuchElementException
|
||||||
|
|
||||||
|
|
||||||
class Tweet:
|
class Tweet:
|
||||||
def __init__(self, card: Chrome) -> None:
|
def __init__(self, card: Chrome) -> None:
|
||||||
self.card = card
|
self.card = card
|
||||||
@@ -73,6 +74,22 @@ class Tweet:
|
|||||||
except NoSuchElementException:
|
except NoSuchElementException:
|
||||||
self.like_cnt = '0'
|
self.like_cnt = '0'
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.analytics_cnt = card.find_element(
|
||||||
|
'xpath',
|
||||||
|
'.//a[contains(@href, "/analytics")]//span'
|
||||||
|
).text
|
||||||
|
except NoSuchElementException:
|
||||||
|
self.analytics_cnt = '0'
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.profile_img = card.find_element(
|
||||||
|
'xpath',
|
||||||
|
'.//div[@data-testid="Tweet-User-Avatar"]//img'
|
||||||
|
).get_attribute('src')
|
||||||
|
except NoSuchElementException:
|
||||||
|
self.profile_img = ''
|
||||||
|
|
||||||
self.tweet = (
|
self.tweet = (
|
||||||
self.user,
|
self.user,
|
||||||
self.handle,
|
self.handle,
|
||||||
@@ -81,7 +98,9 @@ class Tweet:
|
|||||||
self.content,
|
self.content,
|
||||||
self.reply_cnt,
|
self.reply_cnt,
|
||||||
self.retweet_cnt,
|
self.retweet_cnt,
|
||||||
self.like_cnt
|
self.like_cnt,
|
||||||
|
self.analytics_cnt,
|
||||||
|
self.profile_img
|
||||||
)
|
)
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ It may be due to the following:
|
|||||||
- Twitter is experiencing unusual activity
|
- Twitter is experiencing unusual activity
|
||||||
""")
|
""")
|
||||||
self.driver.quit()
|
self.driver.quit()
|
||||||
exit()
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
print("Re-attempting to input username...")
|
print("Re-attempting to input username...")
|
||||||
|
|
||||||
@@ -166,7 +166,7 @@ It may be due to the following:
|
|||||||
- Twitter is experiencing unusual activity
|
- Twitter is experiencing unusual activity
|
||||||
""")
|
""")
|
||||||
self.driver.quit()
|
self.driver.quit()
|
||||||
exit()
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
print("Re-attempting to input password...")
|
print("Re-attempting to input password...")
|
||||||
|
|
||||||
@@ -263,7 +263,9 @@ It may be due to the following:
|
|||||||
'Content': [tweet[4] for tweet in self.data],
|
'Content': [tweet[4] for tweet in self.data],
|
||||||
'Comments': [tweet[5] for tweet in self.data],
|
'Comments': [tweet[5] for tweet in self.data],
|
||||||
'Retweets': [tweet[6] for tweet in self.data],
|
'Retweets': [tweet[6] for tweet in self.data],
|
||||||
'Likes': [tweet[7] for tweet in self.data]
|
'Likes': [tweet[7] for tweet in self.data],
|
||||||
|
'Analytics': [tweet[8] for tweet in self.data],
|
||||||
|
'Profile Image': [tweet[9] for tweet in self.data],
|
||||||
}
|
}
|
||||||
|
|
||||||
df = pd.DataFrame(data)
|
df = pd.DataFrame(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user