-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_trends.py
More file actions
68 lines (59 loc) · 2.24 KB
/
google_trends.py
File metadata and controls
68 lines (59 loc) · 2.24 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import logging
from pytrends.request import TrendReq
import pandas as pd
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Initialize pytrends
pytrends = TrendReq(hl='en-US', tz=360)
# Example normalization factors
population = {
"New York": 8419000,
"Amsterdam": 821752,
"Rio de Janeiro": 6748000,
"Hong Kong": 7507000
}
internet_penetration = {
"New York": 0.95,
"Amsterdam": 0.98,
"Rio de Janeiro": 0.70,
"Hong Kong": 0.93
}
# Define city information
city_info = {
"New York": {
"geo_code": "US-NY",
"keywords": ["Climate change", "Global warming", "Renewable energy", "Carbon footprint", "Paris Agreement"]
},
"Amsterdam": {
"geo_code": "NL-NH",
"keywords": ["Klimaatverandering", "Opwarming van de aarde", "Hernieuwbare energie", "Koolstofvoetafdruk", "Klimaatakkoord van Parijs"]
},
"Rio de Janeiro": {
"geo_code": "BR-RJ",
"keywords": ["Mudança climática", "Aquecimento global", "Energia renovável", "Pegada de carbono", "Acordo de Paris"]
},
"Hong Kong": {
"geo_code": "HK",
"keywords": ["氣候變化", "全球暖化", "可再生能源", "碳足跡", "巴黎協定"]
}
}
# Collect and normalize data for each city
for city, info in city_info.items():
try:
# Fetch data
pytrends.build_payload(kw_list=info["keywords"], timeframe="2004-01-01 2024-12-31", geo=info["geo_code"])
trends_data = pytrends.interest_over_time()
if not trends_data.empty:
trends_data = trends_data[info["keywords"]]
trends_data.reset_index(inplace=True)
trends_data["City"] = city
# Normalize data
for keyword in info["keywords"]:
trends_data[f"{keyword}_normalized"] = trends_data[keyword] / (population[city] * internet_penetration[city])
# Save normalized data
trends_data.to_csv(f"{city.lower().replace(' ', '_')}_trends_normalized.csv", index=False)
print(f"Normalized data successfully collected for {city}.")
else:
print(f"No data available for {city}.")
except Exception as e:
print(f"Error collecting data for {city}: {e}")