import time import traceback import dataset from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from celery import Celery #import redis from celery.schedules import crontab import celery from celery.signals import celeryd_init from celery import signals import random from click import Option from selenium import webdriver import time import sys import os import zipfile from seleniumwire import webdriver import requests from ga4mp import GtagMP from ga4mp import FirebaseMP from ga4mp.store import DictStore import os #from selenium import webdriver PROXY_PORT = 8080 # port PROXY_USER = 'choozmo' # username PROXY_PASS = 'Chuz83102968' # password ups=str(os.urandom(5).hex()).upper()[0:5] print(ups) gtag_tracker = FirebaseMP(api_secret='HoQdcKyKS9SQsjIyphWKQA', firebase_app_id='1:589953727312:android:6923bc306ae616a1da8a55', app_instance_id="8F1C7C2C9A004DECAF27DB6AEFF"+ups) purchase_event = gtag_tracker.create_new_event(name="seo_click") custname=None param=sys.argv[1] rnd=True if len(sys.argv)>=3: rnd=False r = requests.get('http://db.ptt.cx:3001/api/push/'+param+'?status=up&msg=OK&ping=') #U1cGPOJAQD #http://db.ptt.cx:3001/api/push/hzmMjOaHpl?status=up&msg=OK&ping= def selenium_jared_click(): global custname global rnd r=random.randint(0,5) options = {} if r==0: options['proxy']={'http': 'http://choozmo:Chuz83102968@172.105.205.52:8080'} if r==1: options['proxy']={'http': 'http://choozmo:Chuz83102968@139.144.120.184:8080'} if r==2: options['proxy']={'http': 'http://choozmo:Chuz83102968@172.104.92.245:8080'} if r==3: options['proxy']={'http': 'http://choozmo:Chuz83102968@172.104.67.159:8080'} if r==4: options['proxy']={'http': 'http://choozmo:Chuz83102968@139.162.71.115:8080'} print(options) # options = { # 'proxy': { # 'http': 'http://choozmo:Chuz83102968@172.105.205.52:8080' # 'http': 'http://choozmo:Chuz83102968@139.144.120.184:8080' # 'http': 'http://choozmo:Chuz83102968@172.104.92.245:8080' # 'http': 'http://choozmo:Chuz83102968@172.104.67.159:8080' # } # } c_options = webdriver.ChromeOptions() c_options.add_argument("--no-sandbox") c_options.add_argument("--headless") c_options.add_argument("--incognito") db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres') mobile_emulation = { "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 }, "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" } try: if rnd: driver = webdriver.Chrome(seleniumwire_options=options,options=c_options) else: driver = webdriver.Chrome(options=c_options) except: traceback.print_exc() #kw='真理大學國際生' #domain='cia.au.edu.tw' if custname is None: custname='真理' cursor=db.query("SELECT cust,plan,prefix,domain,kw,positive FROM public.seo_jobs where cust='"+custname+"' order by random() limit 1") kw=None domain=None for c in cursor: cust=c['cust'] kw=c['kw'] plan=c['plan'] prefix=c['prefix'] # domain=eval(c['domain'])[0] domains=eval(c['domain']) positive=eval(c['positive']) break #kw='真理大學教堂' #domain='udn.com' print(kw) print(domain) #kw='真理大學校友' #kw='真理大學國際生' #kw='真理大學張聰聯' #domain='au.edu.tw' #domain='pronews.tw' driver.get('https://www.google.com?num=100') time.sleep(3) print(driver.current_url) elmt = driver.find_element(By.XPATH, "//textarea[@name='q']") time.sleep(1) elmt.send_keys(kw) elmt.send_keys(Keys.ENTER) time.sleep(4) elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']") numresults=len(elmts) print('搜尋結果數量',numresults) if numresults<=0: print(elmts) table=db['seo_jobs_ranking_jared'] cnt=1 for elmt in elmts: href=elmt.get_attribute('href') txt=elmt.text # print(txt) # print(href) # if domain in href: for domain in domains: if domain in href: purchase_event.set_event_param(name="query", value=kw) event_list = [purchase_event] gtag_tracker.send(events=event_list) webdriver.ActionChains(driver).move_to_element(elmt).perform() webdriver.ActionChains(driver).move_to_element(elmt).click().perform() print(domain) print(href) print('ranking'+str(cnt)) table.insert({'ranking':cnt,'kw':kw,'results':numresults,'url':href,'title':txt}) driver.quit() return txt break cnt+=1 table.insert({'ranking':-1,'kw':kw,'results':numresults,'url':None,'title':'未收錄'}) return '{empty}' # time.sleep(5) selenium_jared_click()