Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

main.py 2.4 KB

You have to be logged in to leave a comment. Sign In
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
  1. import pytrends
  2. import pandas as pd
  3. import re
  4. import google.auth
  5. from google.cloud import bigquery
  6. import pandas_gbq
  7. import os
  8. from pytrends.request import TrendReq
  9. pytrend = TrendReq()
  10. credentials, your_project_id = google.auth.default(
  11. scopes=["https://www.googleapis.com/auth/cloud-platform"]
  12. )
  13. client = bigquery.Client(
  14. credentials=credentials,
  15. project=your_project_id,
  16. location='[specify location, default = 'US']'
  17. )
  18. def extract():
  19. """
  20. query input keyword from GBQ
  21. """
  22. query_job = client.query("""[query]
  23. """)
  24. results = query_job.result()
  25. return results
  26. def preprocess():
  27. """
  28. Some special characters in keywords need to be preprocessed in advanced
  29. """
  30. df = extract().to_dataframe()
  31. df.keyword_norm = df.keyword.apply(lambda x:x.replace("'",""))
  32. df.keyword_norm =df.keyword_norm.apply(lambda x: x.split(' (')[0])
  33. return df
  34. def main(data, context):
  35. """
  36. extract related queries/topics with input keyword. Filter for last 1 month and specific geography
  37. """
  38. tops=pd.DataFrame([])
  39. risings=pd.DataFrame([])
  40. df = preprocess()
  41. for k,k_norm, country in zip(df.keyword,df.keyword_norm,df.country_code):
  42. pytrend.build_payload(kw_list=[cate2_norm],cat=0, timeframe='today 1-m', geo=country, gprop='')
  43. related_queries = pytrend.related_queries() #or related_queries = pytrend.related_topics()
  44. cate2_queries=related_queries[cate2_norm]
  45. top=cate2_queries['top']
  46. if top is not None:
  47. top.keyword=k
  48. top.country_code = country
  49. tops = tops.append(top,ignore_index=True)
  50. rising=cate2_queries['rising']
  51. if rising is not None:
  52. rising.keyword = k
  53. rising.country_code = country
  54. risings=risings.append(rising,ignore_index=True)
  55. credentials, your_project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
  56. client = bigquery.Client(credentials=credentials,project=your_project_id,location='['specify loaction, default='US')
  57. pandas_gbq.to_gbq(tops,destination_table='[dataset_id.table_id]',project_id='[project_id]',if_exists='replace')
  58. pandas_gbq.to_gbq(risings,destination_table='[dataset_id.table_id]',project_id='[project_id]',if_exists='replace')
  59. return print('channel google trends to GBQ')
  60. if __name__ == "__main__":
  61. main('data','context')
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...