-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_trends.py
42 lines (34 loc) · 1.19 KB
/
google_trends.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import csv
import datetime
import requests
from bs4 import BeautifulSoup
__author__ = 'HK Dambanemuya'
__version__ = 'Python2'
'''
Google Trends:
Code to collect Top 20 Google Daily Search Trends
'''
class Google():
def __init__(self):
self.trends = list()
# Get Top 20 Google Daily Search Trends as HTML request
self.google_trends = requests.get("https://trends.google.com/trends/hottrends/atom/feed")
# Parse HTML response using BeautifulSoup
self.soup = BeautifulSoup(self.google_trends.content,'html.parser')
# Method to pre-process BeautifulSoup object
def get_daily_trends(self):
for item in self.soup.find_all('item'):
self.trends.append(item.title.text.strip().encode('utf-8'))
# Method to save pre-processes search trends
def save_daily_trends(self):
with open(str('Data/Trends/trends_' + datetime.datetime.now().strftime('%m_%d_%y') + '.csv'), 'wb') as f:
wr = csv.writer(f, delimiter="\n")
wr.writerow(self.trends)
def main():
g = Google()
g.get_daily_trends()
g.save_daily_trends()
if __name__ in "__main__":
main()