-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_scrapper.py
executable file
·161 lines (129 loc) · 6.57 KB
/
query_scrapper.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
#!/usr/bin/env python
import requests
import arrow
from scalpl import Cut
from bs4 import BeautifulSoup
from tqdm import tqdm, tnrange
import pandas as pd
import pickle
import click
import re
import urllib
import logging
from tools import slugify, esK3K2_ascii_art
from loguru import logger
def get_search_url_by_day(q, date):
search_url = '%s since:%s until:%s' % (q, date.format('YYYY-MM-DD'), date.shift(days=1).format('YYYY-MM-DD'))
search_url = 'https://mobile.twitter.com/search?q=' + urllib.parse.quote_plus(search_url)
return search_url
def _get_status(soup):
c_statuses = []
tweets = soup.find_all('table', {"class": "tweet"})
#print("Tweets found: %d" % len(tweets))
for cur_tweet in tweets:
if 'tombstone-tweet' in cur_tweet['class']:
# Dead twitter account reference
continue
#soup.find_all('div', {"class": "tweet-text"}):
cur_tweet_data = cur_tweet.find('div', {"class": "tweet-text"})
try:
cur_tweet_text = cur_tweet_data.find('div', {"class": "dir-ltr"})
if cur_tweet_text is None:
cur_tweet_text = cur_tweet_data.get_text()
else:
cur_tweet_text = cur_tweet_text.get_text()
cur_tweet_date = cur_tweet.find('td', {"class": "timestamp"}).find('a').get_text()
if "h" in cur_tweet_date and len(cur_tweet_date) < 4:
hours = int(re.findall("([0-9]{0,2})\s?h", cur_tweet_date)[0])
cur_tweet_date = arrow.get().shift(hours=-hours).format("YYYY-MM-DD")
elif "m" in cur_tweet_date and len(cur_tweet_date) < 4:
hours = int(re.findall("([0-9]{0,2})\s?m", cur_tweet_date)[0])
cur_tweet_date = arrow.get().shift(hours=-hours).format("YYYY-MM-DD")
elif "s" in cur_tweet_date and len(cur_tweet_date) < 4:
hours = int(re.findall("([0-9]{0,2})\s?s", cur_tweet_date)[0])
cur_tweet_date = arrow.get().shift(hours=-hours).format("YYYY-MM-DD")
elif len(cur_tweet_date) <9:
# On current year tweets doesn't show a year in text
cur_tweet_date += arrow.get().format(" YY")
cur_tweet_date = arrow.get(cur_tweet_date,"MMM D YY").format("YYYY-MM-DD")
else:
cur_tweet_date = arrow.get(cur_tweet_date,"D MMM YY").format("YYYY-MM-DD")
c_statuses += [(cur_tweet_data['data-id'],
cur_tweet['href'],
cur_tweet_date,
cur_tweet_text)]
except:
logger.warn ("Not processing: \n %s" % cur_tweet)
return c_statuses
def _get_next_page_link(soup):
"""Search above soup object next page button to return link if no link found returns None
Arguments:
soup {BeautifulSoup} -- Object with html parsed content
Returns:
str -- URL with next page (or None if isn't found)
"""
next_link = soup.find_all('div', {"class": "w-button-more"})
if len(next_link) > 0:
next_page_link = "https://mobile.twitter.com" + next_link[0].find('a')['href']
return next_page_link
else:
return None
@click.command()
@click.option('-q','--query', prompt='Your query', help='Twitter query. You can test it online with weh user interface of twitter.', required=True, type=str)
@click.option('-s','--start_date', help='This script scrappes tweeter by day by day, so you need to set a start date. Format: YYYY-MM-DD')
@click.option('-e','--end_date', help='This script scrappes tweeter by day by day, so you need to set a end date. Format: YYYY-MM-DD')
def __scrape_twitter_by_date(query: str, start_date:str=arrow.get().format('YYYY-MM-DD'), end_date:str=arrow.get().shift(years=-10).format('YYYY-MM-DD')):
"""Simple program that greets NAME for a total of COUNT times. (Command line launch)
Arguments:
query {str} -- Twitter query language expression (can be tested on twitter)
Keyword Arguments:
start_date {str} -- Start date from being requested a twitter query (default: {arrow.get().format('YYYY-MM-DD')})
end_date {str} -- End date from being requested a twitter query (default: {arrow.get().shift(years=-10).format('YYYY-MM-DD')})
"""
df = scrape_twitter_by_date(**locals())
if df is not None:
file_start_date = arrow.get(start_date).format('YYYYMMDD')
file_end_date = arrow.get(end_date).format('YYYYMMDD')
output_filename = "%s_%s--%s.msg" % (file_start_date, file_end_date,slugify(query))
logger.info("Saving results into : %s" % output_filename )
df.to_msgpack(output_filename)
def scrape_twitter_by_date(query: str, start_date:str=arrow.get().format('YYYY-MM-DD'), end_date:str=arrow.get().shift(years=-10).format('YYYY-MM-DD')):
"""Simple program that greets NAME for a total of COUNT times.
Arguments:
query {str} -- Twitter query language expression (can be tested on twitter)
Keyword Arguments:
start_date {str} -- Start date from being requested a twitter query (default: {arrow.get().format('YYYY-MM-DD')})
end_date {str} -- End date from being requested a twitter query (default: {arrow.get().shift(years=-10).format('YYYY-MM-DD')})
"""
cur_date = arrow.get(start_date)
finish_date = arrow.get(end_date)
logger.info("Scrapping 🐦 with:[%s] From 🗓️:[%s] ➡️ To 🗓️:[%s]" % (query, cur_date.format('YYYY-MM-DD'), finish_date.format('YYYY-MM-DD')))
# Create day urls
urls = []
while cur_date <= finish_date:
urls += [get_search_url_by_day(query, cur_date)]
cur_date = cur_date.shift(days=+1)
logger.info("Num requests to send: %d" % len(urls))
statuses = []
for c_url in tqdm(urls):
logger.info("Requesting: %s" % c_url)
res = requests.get(c_url)
soup = BeautifulSoup(res.content,"html.parser")
statuses += _get_status(soup)
next_c_url = _get_next_page_link(soup)
while next_c_url is not None:
next_res = requests.get(next_c_url)
next_soup = BeautifulSoup(next_res.content,"html.parser")
statuses += _get_status(next_soup)
next_c_url = _get_next_page_link(next_soup)
logger.info("Statuses Found: %d" % len(statuses))
if len(statuses) > 0:
df = pd.DataFrame(statuses)
df.columns = ['STATUS_ID', 'TWITTER_HREF', 'TIMESTAMP', 'TEXT']
return df
else:
return None
if __name__ == '__main__':
esK3K2_ascii_art()
print("Twitter Scrapper")
__scrape_twitter_by_date()