1234567891011121314151617181920212223242526272829303132 |
- import pandas as pd
- import dataset
- import pymysql
- pymysql.install_as_MySQLdb()
- name='理茶'
- date = '0216'
- def day():
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
- cursor = db.query(f'select term, url from seo.sns_kw where client ="{name}"')
- kw_lst = []
- for c in cursor:
- kw_lst.append([c['term'],c['url']])
- print(kw_lst)
- df = pd.DataFrame(kw_lst,columns=['kw','url'])
- rank_lst = []
- for i in kw_lst:
- # print(i)
- kw = i[0]
- url = i[1]
- cursor_general = db.query(f'select kw,url,ranking from seo.sns_log where kw = "{kw}" and url="{url}" order by dt desc limit 1')
- for c in cursor_general:
- rank_lst.append([c['kw'],c['url'],c['ranking']])
- db.close()
- df1 = pd.DataFrame(rank_lst,columns=['kw','url','ranking'])
- df_result = pd.merge(df, df1, on=['kw','url'], how='outer').fillna(0)
- df_result.to_csv(f"C:\/Users\/s1301\/Documents\/{date}{name}關鍵字排名.csv", index=False)
- day()
|