Python控制屏幕

Python 控制屏幕教程

下面这部分代码里用到的技术有:

  • Python 链接Sqlserver
  • Python 控制屏幕和鼠标,并实现点击和滚轮事件
  • Python控制屏幕推荐使用 pyautogui 包,这个包里既有控制鼠标又有控制屏幕的功能

效果展示视频

实现代码

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])

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

或与且与或非

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值