Python control screen

Python Control Screen Tutorial

The techniques used in this part of the code below are:

  • Python link to Sqlserver
  • Python controls the screen and mouse, and implements click and wheel events
  • It is recommended to use the pyautogui package to control the screen in Python. This package has the functions of controlling both the mouse and the screen.

Effect display video

Implementation code

from pymouse import PyMouse
import win32api
import win32con
import time
import json
import pyautogui
import pymssql
import pyodbc
from win32clipboard import GetClipboardData, OpenClipboard, CloseClipboard, EmptyClipboard,SetClipboardData

myMouse = PyMouse()


#获取当前的鼠标位置
def getcoord():
	nowP = myMouse.position()

#初次进入门店鼠标坐标
x=1121
y=389
# 进入门店
def enterStore(x,y):
	time.sleep(2)
	myMouse.click(x,y,1,1)
# 退出门店
def exitStore(x,y):
	time.sleep(2)
	myMouse.click(776,147,1,1)
	print("已退出门店")
	#移会门店列表
	myMouse.move(x,y)

def getCopyText():
    OpenClipboard()
    copy_text=GetClipboardData(win32con.CF_UNICODETEXT)
    CloseClipboard()
    return copy_text

def set_clipboard(astr):
    OpenClipboard()
    EmptyClipboard()
    #可以sleep一下,防止操作过快报错
    #time.sleep(1)
    SetClipboardData(win32con.CF_UNICODETEXT, astr)
    CloseClipboard()


list = []
FirestStore=True
storenum=0;
def nextStore(FirestStore,storenum,x,y):
	exitStore(x,y)
	time.sleep(2)
	print("准备进入下一个门店")
	if FirestStore==True:
		win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,-400)
		FirestStore=False
		print(FirestStore)
	else:
			if storenum<=3:
				storenum=storenum+1
				y=y-30
				myMouse.move(x,y)
			else:
				print(FirestStore)
				win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,-50)
	enterStore(x,y)

num=6
Scrollbar=800
loadnum=num*2
def Recorddata(point):
    connect = conn()
    cursor = connect.cursor()
    for i in range(1,loadnum):
        win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,-1*Scrollbar)
        time.sleep(2)
    Ranking=num*20
    for j in range(0,num):
        p=22*j+224
        myMouse.click(668,p,1,1)
        time.sleep(1)
        myMouse.click(604,614,1,1)
        time.sleep(1)
        pyautogui.hotkey('ctrl','a')
        time.sleep(1)
        pyautogui.hotkey('ctrl','c')
        txt=getCopyText()
        for k in json.loads(txt)['data']['poilist']:
            Ranking=0
            AnchorPoint=point
            StoreId=k['id']
            StoreName=k['name']
            Month_sale=k['month_sale']
            Min_price=k['min_price']
            Shipping_fee=k['shipping_fee']
            if Shipping_fee <= 0:
                Shipping_fee=0
            Shipping_fee_tip=k['shipping_fee_tip']
            Simple_name=k['simple_name']
            print("%s已插入" % (StoreName))
            sql = ("insert into BigData_StoreInfo (StoreId,StoreName, Month_sale,Ranking,Min_price,Shipping_fee,shipping_fee_tip,AnchorPoint,Simple_name) values('%s','%s',%s,%s,%s,%s,'%s','%s','%s')"%(StoreId,StoreName,Month_sale,Ranking,Min_price,Shipping_fee,Shipping_fee_tip,AnchorPoint,Simple_name)).encode("utf-8")
            cursor.execute(sql)
            
            
    connect.commit()
    cursor.close()   
    connect.close()
        #myMouse.click(1650,200,1,1)
        #pyautogui.hotkey('ctrl','v')
        # pyautogui.typewrite(['down'])

def conn():
    connect = pymssql.connect('数据库IP', '登录名', '密码','库名',charset='utf8')
  #  connect = pymssql.connect(host='数据库IP', user='登录名称', password='密码', database='库名', charset='utf8')
    if connect:
        print('连接成功!')
        return connect
    else:
        print('连接失败!')

def GetAddress():
    connect = conn()
    cursor = connect.cursor()  
    sql = "select top 100 [Address] from AddRessInfo where isDelete=0" 
    cursor.execute(sql)   #执行sql语句
    row = cursor.fetchone()  #读取查询结果,
    while row:
        print("地址名称=%s" % (row[0]))
        list.append(row[0])
        row = cursor.fetchone()
    cursor.close()   
    connect.close()
    
def nextAddress(astr):
    time.sleep(10)
    myMouse.move(1336,255)
    for i in range(1,loadnum):
        win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,Scrollbar)
        time.sleep(3)
    myMouse.click(1238,87,1,1)
    time.sleep(3)
    myMouse.click(1238,87,1,1)
    set_clipboard(astr)
    pyautogui.hotkey('ctrl','v')
    time.sleep(2)
    myMouse.click(1238,120,1,1)


nowP=getcoord()
#print(nowP)
#鼠标移动到坐标(x,y)处
#myMouse.move(600,800)
#鼠标点击,x,y是坐标位置 button 1表示左键,2表示点击右键 n是点击次数,默认是1次,2表示双击
#myMouse.click(1336,255,1,1)
#print("点击事件完成")
#time.sleep(3)



GetAddress()
for astr in list:
    if(astr!=''):
        #txt=getCopyText()
        #print("剪切板内容=%s" % (txt))
        #nextAddress(astr)
        Recorddata(astr)


#进入第一个门店
#enterStore(nowP[0],nowP[1])


#nextStore(FirestStore,storenum,nowP[0],nowP[1])
#nextStore(FirestStore,storenum,nowP[0],nowP[1])
#nextStore(FirestStore,storenum,nowP[0],nowP[1])

Guess you like

Origin blog.csdn.net/qq_42455262/article/details/127565869