-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext_scraper.py
104 lines (97 loc) · 2.84 KB
/
text_scraper.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
'''
scraper based on text (ie from select all and copy on a website). Not as good as html, but more adapatble and less impacted by change
'''
import re
fb_posts={}
fi = 0
ti = 0
tweets = {}
tweet_filter_set = {'·'}
def clear():
global fb_posts,tweets,fi,ti
fb_posts = {}
fi = 0
ti = 0
tweets = {}
def scrape_tweets(txt,acc_name=''):
global ti
# startPostReg = r"\d\w{,7}•"
# • instead of • in html
startPostReg = r" .{,20}Retweeted$"
endPostReg = r"^Feed post$|^Affiliated pages$|^You might like$"
post = ''
post_i = 0
inPost = False
for line,nxt_line in txt.splitlines():
# TODO: tighten this
if re.match(startPostReg, line):
inPost = True
elif inPost and (re.match(endPostReg, line) or (line[0] == '@' and line[1:]==acc_name)):
inPost = False
if not (post in posts):
posts[i] = post.rstrip('\n')
i += 1
post = ''
if inPost and not (line in tweet_filter_set):
post += line + '\n'
return posts
def scrape_fb_company_posts(txt):
global fi
#startPostReg = r"\d\w{,7}•"
# • instead of • in html
#startPostReg = r"^ ·$"
startPostReg = r"^[\w ].{,40}· ?$"
endPostReg = startPostReg #r"[\d:]* \/ [\d:]*"
seeMorePost = r"See more$"
spaces = 0
post = ''
post_i = 0
inPost = False
for line in txt.splitlines():
#skip See More
#if re.match(seeMorePost,line):
# post = ''
# inPost = False
# break
# TODO: tighten this
if re.match(startPostReg,line) and not inPost:
inPost = True
elif inPost and re.match(endPostReg,line): #spaces == 2:
inPost = False
if not (post in posts):
fb_posts[fi] = post.rstrip('\n')
fi += 1
post = ''
if inPost and not (line in filter_set):
post += line + '\n'
return fb_posts
filter_set = {'Your document has finished loading'
'Your document is loading'}
posts={}
'''
<span class="break-words">
<span dir="ltr">
'''
i = 0
def scrape_linkedin_posts(txt):
global i
#startPostReg = r"\d\w{,7}•"
# • instead of • in html
startPostReg = r" \d.{,15}ago"
endPostReg = r"^Feed post$|^Affiliated pages$|^Loading more results$"
post = ''
post_i = 0
inPost = False
for line in txt.splitlines():
# TODO: tighten this
if re.match(startPostReg,line):
inPost = True
elif inPost and re.match(endPostReg,line):
inPost = False
if not (post in posts):
posts[i] = post.rstrip('\n')
i += 1
post = ''
if inPost and not (line in filter_set):
post += line + '\n'
return posts