-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprac.py
170 lines (129 loc) · 3.95 KB
/
prac.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'''
https://twitter.com/search?q=%EB%AC%B8%EC%9E%AC%EC%9D%B8
since%3A2017-06-01%20until%3A2017-06-06&src=typd
'''
'''
https://twitter.com/search?l=&q=%EB%AC%B8%EC%9E%AC%EC%9D%B8
%20until%3A2017-06-08&src=typd&lang=ko
'''
import urllib.request as ur
from bs4 import BeautifulSoup
from datetime import datetime
import json
tweet = {}
epoch = 15
headers = {}
headers['User-Agent'] = "chrome/5.0"
#url = "https://twitter.com/search?q=%EB%AC%B8%EC%9E%AC%EC%9D%B8&src=typd&lang=ko"
_url = "https://twitter.com/search?l=&q=%EB%AC%B8%EC%9E%AC%EC%9D%B8%20until%3A"
dat = "2017-05-30"
url_end = "&src=typd&lang=ko"
#url = _url + dat + url_end
# URL INFO
n= 2
while(n):
n = n-1
url = _url + dat + url_end
print(n)
req = ur.Request(url, headers = headers)
fp = ur.urlopen(req)
# (js-tweet-text / 2) 갯수 세면 된다.
#print(fp.read())
# TODO
'''
https://twitter.com/
search?q=%EB%AC%B8%EC%9E%AC%EC%9D%B8&src=typd&
lang=ko
'''
soup = BeautifulSoup(fp, 'html.parser')
#print(soup.prettify())
#print(soup.find_all("js-tweet-text-container"))
myDiv = soup.find_all("div", { "class" : "js-tweet-text-container"})
#print(soup.find_all("div"))
print(soup.findAll(lambda tag: tag.has_attr('data-time')))
#print(myDiv)
#print(myDiv[0])
p = soup.find_all("div", { "class" : "tweet" })
print("HEREREREREEREREREERERE")
aaa = p[0].find(lambda tag: tag.has_attr('data-time'))['data-time']
tweet[aaa] = 0
print("KJLFJDALFJADKLFJDLKFJ")
print(tweet)
ppp = p[0]["data-reply-to-users-json"]
ppp = json.loads(ppp)[0]
print(ppp["screen_name"])
print(ppp)
p = p[0:epoch]
# TIME
k = soup.find_all("div", { "class": "stream-item-header"})
t = k[0].find("a",{"class": "tweet-timestamp"})['title']
if(t[0:2] == "오후"):
m = t.replace("오후", "PM")
else:
m = t.replace("오전", "AM")
m = datetime.strptime(m, "%p %I:%M - %Y년 %m월 %d일")
mx = m
print(m)
for i in k:
u = i.find("a", {"class": "tweet-timestamp"})
if(u is not None):
tmp = u['title']
if(tmp[0:2] == "오후"):
tmp = u['title'].replace("오후", "PM")
elif(tmp[0:2] == "오전"):
tmp = u['title'].replace("오전", "AM")
print(tmp)
tmp = datetime.strptime(tmp, "%p %I:%M - %Y년 %m월 %d일")
if(m > tmp):
m = tmp
if(mx < tmp):
mx = tmp
tt = m.timetuple()
next_date_url = str(tt.tm_year) + "-" + str(tt.tm_mon) + "-" + str(tt.tm_mday)
print("MINIMUM")
print(m)
print(next_date_url)
dat = next_date_url
print(mx)
# TEXT
#q = soup.find("div", { "class" : "stream-item-footer" })
total_retweet = 0
for i in p:
tmp = i.find("button", {"class" : "js-actionRetweet"})
if(tmp == None):
continue
#print(tmp)
t = tmp.find("span", {"class": "ProfileTweet-actionCountForPresentation"})
a = t.text
if(a == ''):
continue
a = a.replace(',', '')
total_retweet += int(a)
print("SUM")
print(total_retweet)
total_reply = 0
for i in p:
tmp = i.find("button", {"class" : "js-actionReply"})
if(tmp == None):
continue
t = tmp.find("span", {"class": "ProfileTweet-actionCountForPresentation"})
a = t.text
if(a == ''):
continue
a = a.replace(',', '')
total_reply += int(a)
print("SUM")
print(total_reply)
total_fav = 0
for i in p:
tmp = i.find("button", {"class" : "js-actionFavorite"})
if(tmp == None):
continue
t = tmp.find("span", {"class": "ProfileTweet-actionCountForPresentation"})
a = t.text
if(a == ''):
continue
a = a.replace(',', '')
total_fav += int(a)
print("SUM")
print(total_fav)