-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.py
More file actions
126 lines (113 loc) · 5.79 KB
/
twitter.py
File metadata and controls
126 lines (113 loc) · 5.79 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- coding: utf-8 -*-
from selenium import webdriver
from bs4 import BeautifulSoup
import os, sys, time
import requests, json, csv
from datetime import datetime
class TwitterHelper():
def __init__(self):
self.main_url = "https://twitter.com"
self.driver = webdriver.Chrome('chromedriver.exe')
self.today_date = datetime.today()
# method to get Twitter data.
def getTwitterData(self):
items = []
job_url = self.main_url + "/search?f=tweets&vertical=default&q=app%20developer&src=tyah"
self.driver.get(job_url)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
tweets = self.driver.find_elements_by_xpath("//ol[@id='stream-items-id']/li")
print("Total tweets : ",len(tweets))
if len(tweets):
for tweet in tweets:
try:
tweet_id = tweet.get_attribute("data-item-id")
author_name = tweet.find_element_by_xpath(".//span[@class='FullNameGroup']/strong")
if author_name:
author_name = str(author_name.text).encode("utf-8")
username = tweet.find_element_by_xpath(".//span[@class='username u-dir u-textTruncate']")
if username:
username = str(username.text).replace("@", '').strip()
author_img = tweet.find_element_by_xpath(".//img[@class='avatar js-action-profile-avatar']").get_attribute("src")
tweet_at = tweet.find_element_by_xpath(".//span[@class='_timestamp js-short-timestamp js-relative-timestamp']").text
# reply_to = tweet.find_element_by_xpath(".//div[@class='ReplyingToContextBelowAuthor']")
# if reply_to:
# reply_to = reply_to.text
tweet = tweet.find_element_by_xpath(".//div[@class='js-tweet-text-container']")
if tweet:
tweet = str(tweet.text).encode("utf-8")
author_url = self.main_url + "/" + str(username)
res = requests.get(author_url)
soup = BeautifulSoup(res.content, u'html.parser')
tweets = soup.find("a", attrs={"data-nav": "tweets"})
if tweets:
tweets = tweets.find("span", attrs={"class": "ProfileNav-value"})['data-count']
followings = soup.find("a", attrs={"data-nav": "following"})
if followings:
followings = followings.find("span", attrs={"class": "ProfileNav-value"})['data-count']
followers = soup.find("a", attrs={"data-nav": "followers"})
if followers:
followers = followers.find("span", attrs={"class": "ProfileNav-value"})['data-count']
favorites = soup.find("a", attrs={"data-nav": "favorites"})
if favorites:
favorites = favorites.find("span", attrs={"class": "ProfileNav-value"})['data-count']
lists = soup.find("a", attrs={"data-nav": "all_lists"})
if lists:
lists = lists.find("span", attrs={"class": "ProfileNav-value"}).text
else:
lists = ""
location = soup.find("div", attrs={"class": "ProfileHeaderCard-location"})
if location:
location = str(location.text).strip()
items.append({
"tweet_id": tweet_id,
"author_name": author_name,
"author_img": author_img,
"username": username,
"tweet_at": tweet_at,
"tweet": tweet,
"tweets": tweets,
"followings": followings,
"followers": followers,
"favorites": favorites,
"lists": lists,
"location": location,
})
except:
pass
self.driver.close()
return items
# method to write csv file.
def writeCSV(self, items):
try:
with open("twitter.csv", mode='w') as csv_file:
fieldnames = ["Twitter Id", "Full Name", "Username", "Image", "Location", "Follwers", "Followings", "Favorites", "Lists", "Tweet", "Created At"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for item in items:
writer.writerow(
{
'Twitter Id': item['tweet_id'], 'Full Name': item['author_name'], 'Username': item['username'],
'Image': item['author_img'], 'Location': item['location'], 'Follwers': item['followers'],
'Followings': item['followings'], 'Favorites': item['favorites'], 'Lists': item['lists'],
'Tweet': item['tweet'], 'Created At': item['tweet_at']
}
)
csv_file.close()
print("File written successfully.")
except:
print("Error occured during written file.")
print(sys.exc_info())
# method to start process.
def start(self):
items = self.getTwitterData()
print("Total items : ",len(items))
if len(items):
self.writeCSV(items)
else:
print("Item not found.")
print("#" * 100)
if __name__ == "__main__":
# objTH is an instance for TwitterHelper.
objTH = TwitterHelper()
objTH.start()