-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscrape_comments.py
More file actions
47 lines (40 loc) · 1.31 KB
/
Copy pathscrape_comments.py
File metadata and controls
47 lines (40 loc) · 1.31 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
import urllib
import urllib2
from xml.dom import minidom
data = None
headers = {
'GData-Version' : 2
}
spamfile = open('spam', 'w')
notspamfile = open('notspam', 'w')
# get list of most popular videos
url = 'http://gdata.youtube.com/feeds/api/videos?max-results=50&orderby=viewCount'
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
xmldata = response.read()
videos = minidom.parseString(xmldata)
video_entries = videos.getElementsByTagName("gd:feedLink")
for entry in video_entries:
link = entry.attributes["href"].value
print "processing: " + link
# get comments
start = 1
while start<=1000:
url = link + '?max-results=50&start-index=' + str(start)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
xmldata = response.read()
comments = minidom.parseString(xmldata)
entries = comments.getElementsByTagName("entry")
for entry in entries:
content = entry.getElementsByTagName("content")
spam = entry.getElementsByTagName("yt:spam")
if len(spam) > 0:
spamfile.write(content[0].firstChild.nodeValue.encode('utf8'))
spamfile.write('\nbbSEP\n')
else:
notspamfile.write(content[0].firstChild.nodeValue.encode('utf8'))
notspamfile.write('\nbbSEP\n')
start += 50
spam.close()
notspam.close()