From 66f5554ca0ec2c3bf9cc68cf34c0022ac286eb94 Mon Sep 17 00:00:00 2001 From: luwc Date: 2018年7月16日 17:30:53 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E7=BD=91=E6=98=93=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/netease.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) mode change 100644 => 100755 src/netease.py diff --git a/src/netease.py b/src/netease.py old mode 100644 new mode 100755 index a95ddf8..4b60473 --- a/src/netease.py +++ b/src/netease.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import sys,os +import sys, os def change_file(filepath): @@ -15,9 +15,26 @@ def change_file(filepath): print('转换完成:%d' % cou) -if __name__ == '__main__': - files = sys.argv[1:] - for filepath in files: - change_file(filepath) +def change_file1(source, targe): + with open(source, 'rb') as file: + print('打开文件%s ' % file.name) + with open(targe, 'wb') as nfile: + print('新文件 %s' % nfile.name) + b = file.read() + a = bytes([by ^ 0xA3 for by in b]) + nfile.write(a) + print('转换完成') +if __name__ == '__main__': + args = sys.argv[1:] + if args.__len__() < 2: + change_file(args[0]) + else: + source = args[0] + targe = args[1] + files = os.listdir(source) + for file in files: + fileName = os.path.splitext(file) + if fileName[1] == '.uc!': + change_file1(os.path.join(source, file), os.path.join(targe, fileName[0] + '.mp3')) From bc278f59f6cf4d76bd646cc89bafb4a5f090cd93 Mon Sep 17 00:00:00 2001 From: luwc Date: 2019年8月21日 22:38:47 +0800 Subject: [PATCH 02/10] =?UTF-8?q?python=E7=88=AC=E8=99=AB=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mp3info.py | 4 ++++ src/urltest.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/mp3info.py create mode 100755 src/urltest.py diff --git a/src/mp3info.py b/src/mp3info.py new file mode 100644 index 0000000..7231c3c --- /dev/null +++ b/src/mp3info.py @@ -0,0 +1,4 @@ +import ID3 + +import os,sys + diff --git a/src/urltest.py b/src/urltest.py new file mode 100755 index 0000000..17c5340 --- /dev/null +++ b/src/urltest.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +from urllib import request +from bs4 import BeautifulSoup +from selenium import webdriver,common + +urlopen = request.urlopen + + +# 实例1 +def cnblogs(): + html = urlopen('https://www.cnblogs.com/Lijcyy/p/9778318.html') + bsobj = BeautifulSoup(html, features="lxml") + postList = bsobj.findAll('div', {'class': 'postBody'}) + for post in postList: + print(post) + + +def cna(): + html = urlopen("https://www.cnblogs.com/Lijcyy/p/9778318.html") + bsobj = BeautifulSoup(html, features="lxml") + # for link in bsobj.findAll('a'): + # if 'href' in link.attrs: + # print(link.attrs['href']) + print(bsobj) + + +if __name__ == '__main__': + options = webdriver.ChromeOptions() + options.add_experimental_option('prefs',{ + 'profile.default_content_settings': { + 'images': 2 + }, + 'profile.managed_default_content_settings': { + 'images': 2 + } + }) + # options.headless = True + # options.add_argument('--disable-gpu') + broswer = webdriver.Chrome(executable_path='/Users/meicloud/apps/bin/chromedriver', chrome_options=options) + try: + broswer.get('https://www.cnblogs.com/Lijcyy/p/9778318.html') + broswer.execute_script('window.open()') + broswer.switch_to.window(broswer.window_handles[1]) + broswer.get('https://www.baidu.com') + broswer.switch_to.window(broswer.window_handles[0]) + bsobj = BeautifulSoup(broswer.page_source, features="lxml") + print('页面加载完成') + + broswer.quit() + for link in bsobj.findAll('a'): + if 'href' in link.attrs: + print(link.attrs['href']) + except common.exceptions.TimeoutException: + broswer.execute_script('window.stop()') From 52f9d978247b45fce4e7ce972a708e5ac1d717c2 Mon Sep 17 00:00:00 2001 From: luwc Date: 2019年8月22日 18:05:12 +0800 Subject: [PATCH 03/10] =?UTF-8?q?githup=E7=88=AC=E8=99=AB=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/githubTending.py | 35 +++++++++++++++++++++++++++++ src/urltest.py | 52 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 src/githubTending.py diff --git a/src/githubTending.py b/src/githubTending.py new file mode 100644 index 0000000..d8c0db1 --- /dev/null +++ b/src/githubTending.py @@ -0,0 +1,35 @@ +import csv +import sys + +import requests +from bs4 import BeautifulSoup + + +def getTending(url, params): + rep = requests.get(url, params) + bsobj = BeautifulSoup(rep.content, features="lxml") + csvfile = open('./githubtend.csv', 'w+') + try: + writer = csv.writer(csvfile) + writer.writerow(('user / name', 'url', 'desc', 'stars', 'incr')) + for art in bsobj.findAll('article', {'class': 'Box-row'}): + star = art.find('svg', {'aria-label': 'star'}).parent.text.strip() + newstar = art.findAll('svg', {'class': ['octicon', 'octicon-star'], 'aria-hidden': 'true'})[ + 2].parent.text.strip() + if art.p: + ps = art.p.text.strip() + else: + ps = '' + print('%s 地址: https://github.com%s \n说明: %s \n星星数:%s 增长数:%s' % ( + art.h1.text.strip(), art.h1.a['href'], ps, star, newstar)) + writer.writerow((art.h1.text.strip(), 'https://github.com' + art.h1.a['href'], ps, star, newstar)) + finally: + csvfile.close() + + +if __name__ == '__main__': + args = sys.argv[1:] + if len(args): + getTending('https://github.com/trending', {'since': args[0]}) + else: + getTending('https://github.com/trending', {'since': 'weekly'}) diff --git a/src/urltest.py b/src/urltest.py index 17c5340..5a5337f 100755 --- a/src/urltest.py +++ b/src/urltest.py @@ -2,7 +2,10 @@ from urllib import request from bs4 import BeautifulSoup -from selenium import webdriver,common +from selenium import webdriver, common +from time import sleep +import requests +import csv urlopen = request.urlopen @@ -16,18 +19,17 @@ def cnblogs(): print(post) -def cna(): - html = urlopen("https://www.cnblogs.com/Lijcyy/p/9778318.html") +def cna(url): + html = urlopen(url) bsobj = BeautifulSoup(html, features="lxml") - # for link in bsobj.findAll('a'): - # if 'href' in link.attrs: - # print(link.attrs['href']) - print(bsobj) + for link in bsobj.findAll('a'): + if 'href' in link.attrs: + print(link.attrs['href']) -if __name__ == '__main__': +def seleniumtest(): options = webdriver.ChromeOptions() - options.add_experimental_option('prefs',{ + options.add_experimental_option('prefs', { 'profile.default_content_settings': { 'images': 2 }, @@ -35,11 +37,14 @@ def cna(): 'images': 2 } }) + options.add_experimental_option('excludeSwitches', ['enable-automation']) # 切换到开发者模式 # options.headless = True # options.add_argument('--disable-gpu') broswer = webdriver.Chrome(executable_path='/Users/meicloud/apps/bin/chromedriver', chrome_options=options) try: - broswer.get('https://www.cnblogs.com/Lijcyy/p/9778318.html') + broswer.get('https://www.zhihu.com/topic/19574589/hot') + sleep(20) + broswer.execute_script('window.open()') broswer.switch_to.window(broswer.window_handles[1]) broswer.get('https://www.baidu.com') @@ -51,5 +56,32 @@ def cna(): for link in bsobj.findAll('a'): if 'href' in link.attrs: print(link.attrs['href']) + print(link.text) except common.exceptions.TimeoutException: broswer.execute_script('window.stop()') + + +def getTending(url, params): + rep = requests.get(url, params) + bsobj = BeautifulSoup(rep.content, features="lxml") + csvfile = open('./githubtend.csv', 'w+') + try: + writer = csv.writer(csvfile) + writer.writerow(('user / name', 'url', 'desc', 'stars', 'incr')) + for art in bsobj.findAll('article', {'class': 'Box-row'}): + star = art.find('svg', {'aria-label': 'star'}).parent.text.strip() + newstar = art.findAll('svg', {'class': ['octicon', 'octicon-star'], 'aria-hidden': 'true'})[ + 2].parent.text.strip() + if art.p: + ps = art.p.text.strip() + else: + ps = '' + print('%s 地址: https://github.com%s \n说明: %s \n星星数:%s 增长数:%s' % ( + art.h1.text.strip(), art.h1.a['href'], ps, star, newstar)) + writer.writerow((art.h1.text.strip(), 'https://github.com' + art.h1.a['href'], ps, star, newstar)) + finally: + csvfile.close() + + +if __name__ == '__main__': + getTending('https://github.com/trending', {'since': 'weekly'}) From a72a9b9dc76aee2542c463d2d8b06588d1c322a6 Mon Sep 17 00:00:00 2001 From: LUMR Date: 2019年8月22日 21:40:40 +0800 Subject: [PATCH 04/10] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5269fa6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# studyPython +学习python过程中写的代码 From 700f6c072d43af7642387c7ad42cccb9329f9285 Mon Sep 17 00:00:00 2001 From: lumr Date: 2019年8月22日 23:51:17 +0800 Subject: [PATCH 05/10] =?UTF-8?q?lagou=E6=90=9C=E7=B4=A2=E7=BD=91=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lagouSearch.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/lagouSearch.py diff --git a/src/lagouSearch.py b/src/lagouSearch.py new file mode 100644 index 0000000..d756718 --- /dev/null +++ b/src/lagouSearch.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +import csv +import sys +from time import sleep +import requests +from bs4 import BeautifulSoup +from selenium import webdriver, common + + +def createBrowser(): + options = webdriver.ChromeOptions() + options.add_experimental_option('prefs', { + 'profile.default_content_settings': { + 'images': 2 + }, + 'profile.managed_default_content_settings': { + 'images': 2 + } + }) + return webdriver.Chrome(executable_path='$HOME/apps/bin/chromedriver', chrome_options=options) + + +def search(): + url = 'https://www.lagou.com/zhaopin/Java/' + browser = createBrowser() + browser.get('%s%s' % (url, '')) + rs = browser.find_element_by_class_name('city-wrapper') + rs.find_element_by_link_text('广州').click() + sleep(5) + rs = browser.find_element_by_id('switchCity') + rs.find_element_by_link_text('广州站').click() + sleep(5) + for i in list(range(29)): + browser.get('%s%s' % (url, str(i + 1))) + sleep(6) + ansyHtml(browser.page_source, i) + sleep(5) + browser.quit() + + +def ansyHtml(source, i): + html = BeautifulSoup(source, features='lxml') + list = html.find('div', {'id': 's_position_list'}).ul.findAll('li') + with open('$HOME/Local/lagou_%s.html' % str(i), 'w+') as f: + f.write(source) + print('打印第%s页完成' % str(i + 1)) + + +if __name__ == '__main__': + search() From eabe309a1d0b6a77a02d8a5efa53a548183fe422 Mon Sep 17 00:00:00 2001 From: luwc Date: 2019年8月23日 15:21:43 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=86=E6=9E=90?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 113 ++++++++++++++++++++++++++++++++++++++++++- src/githubTending.py | 8 ++- src/lagouSearch.py | 53 +++++++++++++++++--- 3 files changed, 159 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 6b75623..b3b5e34 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,111 @@ -.idea -venv \ No newline at end of file +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +#Mac +.DS_Store + +#ide +.idea/ +.vscode/ \ No newline at end of file diff --git a/src/githubTending.py b/src/githubTending.py index d8c0db1..75d4e72 100644 --- a/src/githubTending.py +++ b/src/githubTending.py @@ -8,8 +8,7 @@ def getTending(url, params): rep = requests.get(url, params) bsobj = BeautifulSoup(rep.content, features="lxml") - csvfile = open('./githubtend.csv', 'w+') - try: + with open('./githubtend.csv', 'w+') as csvfile: writer = csv.writer(csvfile) writer.writerow(('user / name', 'url', 'desc', 'stars', 'incr')) for art in bsobj.findAll('article', {'class': 'Box-row'}): @@ -22,9 +21,8 @@ def getTending(url, params): ps = '' print('%s 地址: https://github.com%s \n说明: %s \n星星数:%s 增长数:%s' % ( art.h1.text.strip(), art.h1.a['href'], ps, star, newstar)) - writer.writerow((art.h1.text.strip(), 'https://github.com' + art.h1.a['href'], ps, star, newstar)) - finally: - csvfile.close() + writer.writerow( + (art.h1.text.strip(), 'https://github.com' + art.h1.a['href'], ps, star, newstar)) if __name__ == '__main__': diff --git a/src/lagouSearch.py b/src/lagouSearch.py index d756718..ff0cb20 100644 --- a/src/lagouSearch.py +++ b/src/lagouSearch.py @@ -2,9 +2,10 @@ import csv import sys from time import sleep + import requests -from bs4 import BeautifulSoup -from selenium import webdriver, common +from bs4 import BeautifulSoup, element +from selenium import common, webdriver def createBrowser(): @@ -17,7 +18,7 @@ def createBrowser(): 'images': 2 } }) - return webdriver.Chrome(executable_path='$HOME/apps/bin/chromedriver', chrome_options=options) + return webdriver.Chrome(executable_path='/Users/meicloud/apps/bin/chromedriver', chrome_options=options) def search(): @@ -30,7 +31,7 @@ def search(): rs = browser.find_element_by_id('switchCity') rs.find_element_by_link_text('广州站').click() sleep(5) - for i in list(range(29)): + for i in list(range(30)): browser.get('%s%s' % (url, str(i + 1))) sleep(6) ansyHtml(browser.page_source, i) @@ -41,10 +42,46 @@ def search(): def ansyHtml(source, i): html = BeautifulSoup(source, features='lxml') list = html.find('div', {'id': 's_position_list'}).ul.findAll('li') - with open('$HOME/Local/lagou_%s.html' % str(i), 'w+') as f: - f.write(source) - print('打印第%s页完成' % str(i + 1)) + for li in list: + industry = li.find('div', {'class': 'industry'}).text.strip() + tag = li.find('div', {'class': 'li_b_r'}).text.strip() + require = li.find('span', {'class': 'money'} + ).parent.contents[4].strip() + link = li.find('a', {'class': 'position_link'})['href'] + info = li.attrs.copy() + info['industry'] = industry + info['tag'] = tag + info['require'] = require + info['link'] = link + print(info) + + +def ansyLocal(): + for i in list(range(29)): + with open('/Users/meicloud/Local/lagou_%s.html'% str(i), 'r') as f: + html = BeautifulSoup(f.read(), features='lxml') + lis = html.find('div', {'id': 's_position_list'}).ul.findAll('li') + for li in lis: + industry = li.find('div', {'class': 'industry'}).text.strip() + tag = li.find('div', {'class': 'li_b_r'}).text.strip() + require = li.find('span', {'class': 'money'} + ).parent.contents[4].strip() + link = li.find('a', {'class': 'position_link'})['href'] + info = li.attrs.copy() + info['industry'] = industry + info['tag'] = tag + info['require'] = require + info['link'] = link + print(info) + saveResult(info) + +num = 1 +def saveResult(result): + global num + resp = requests.post('http://luwc002.cn.midea.com:9200/works/_doc/%s'% str(num),json=result) + print(resp) + num = num + 1 if __name__ == '__main__': - search() + ansyLocal() \ No newline at end of file From 2380d0779b6aa592a280069ec8c776a1abf6eb40 Mon Sep 17 00:00:00 2001 From: lumr Date: 2019年8月26日 21:42:23 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lagouSearch.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lagouSearch.py b/src/lagouSearch.py index ff0cb20..952bf09 100644 --- a/src/lagouSearch.py +++ b/src/lagouSearch.py @@ -18,7 +18,7 @@ def createBrowser(): 'images': 2 } }) - return webdriver.Chrome(executable_path='/Users/meicloud/apps/bin/chromedriver', chrome_options=options) + return webdriver.Chrome(executable_path='/Users/meicloud/apps/bin/chromedriver', options=options) def search(): @@ -41,8 +41,8 @@ def search(): def ansyHtml(source, i): html = BeautifulSoup(source, features='lxml') - list = html.find('div', {'id': 's_position_list'}).ul.findAll('li') - for li in list: + lis = html.find('div', {'id': 's_position_list'}).ul.findAll('li') + for li in lis: industry = li.find('div', {'class': 'industry'}).text.strip() tag = li.find('div', {'class': 'li_b_r'}).text.strip() require = li.find('span', {'class': 'money'} @@ -53,19 +53,19 @@ def ansyHtml(source, i): info['tag'] = tag info['require'] = require info['link'] = link - print(info) + saveResult(info) def ansyLocal(): for i in list(range(29)): - with open('/Users/meicloud/Local/lagou_%s.html'% str(i), 'r') as f: + with open('/Users/meicloud/Local/lagou_%s.html' % str(i), 'r') as f: html = BeautifulSoup(f.read(), features='lxml') lis = html.find('div', {'id': 's_position_list'}).ul.findAll('li') for li in lis: industry = li.find('div', {'class': 'industry'}).text.strip() tag = li.find('div', {'class': 'li_b_r'}).text.strip() require = li.find('span', {'class': 'money'} - ).parent.contents[4].strip() + ).parent.contents[4].strip() link = li.find('a', {'class': 'position_link'})['href'] info = li.attrs.copy() info['industry'] = industry @@ -75,13 +75,17 @@ def ansyLocal(): print(info) saveResult(info) + num = 1 + + def saveResult(result): global num - resp = requests.post('http://luwc002.cn.midea.com:9200/works/_doc/%s'% str(num),json=result) + resp = requests.post('http://localhost:9200/works/_doc/%s' % str(num), json=result) print(resp) num = num + 1 if __name__ == '__main__': - ansyLocal() \ No newline at end of file + # ansyLocal() + search() \ No newline at end of file From 50767bada02db9b3713164c4429a1624df27c29c Mon Sep 17 00:00:00 2001 From: lumr Date: 2019年9月28日 10:18:10 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E8=B0=83=E8=AF=95=E9=BC=A0=E6=A0=87?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bilanauto.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/bilanauto.py diff --git a/src/bilanauto.py b/src/bilanauto.py new file mode 100644 index 0000000..60918be --- /dev/null +++ b/src/bilanauto.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import time +from subprocess import call +import cv2 + +import pykeyboard +import pymouse + + +class MouseController: + def __init__(self, mouse): + self.m = mouse + + def position(self): + print('鼠标当前位置是:%s,%s' % self.m.position()) + + def click(self, x, y, button=1, times=1): + if button == 2: + self.rclick(x, y) + else: + self.lclick(x, y, times) + self.position() + + def lclick(self, x, y, times=1): + self.m.click(x, y, 1, times) + + def rclick(self, x, y): + self.m.press(x, y, 2) + time.sleep(0.1) + self.m.release(x, y, 2) + + +fileName = '/Users/lumr/tmp/test.jpg' + +if __name__ == '__main__': + call(['screencapture', '-tjpg', '/Users/lumr/tmp/test.jpg']) + cv2.imread(fileName) + m = MouseController(pymouse.PyMouse()) + k = pykeyboard.PyKeyboard() + # print('鼠标当前位置是:%s,%s' % m.position()) + m.click(600, 500, 2) + + # m.click(600, 500, 2, 2) + # m.press(720, 450, 2) + time.sleep(0.1) + # m.release(720, 450, 2) From b8967d8bfab2315408f5344e83c4a2486f94c65f Mon Sep 17 00:00:00 2001 From: lumr Date: 2019年9月28日 12:03:57 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E5=9B=BE=E5=83=8F=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bilanauto.py | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/bilanauto.py b/src/bilanauto.py index 60918be..4ee2e01 100644 --- a/src/bilanauto.py +++ b/src/bilanauto.py @@ -4,7 +4,14 @@ import cv2 import pykeyboard -import pymouse +from pymouse import PyMouse + +# 图片路径 +PATH = '/Users/lumr/tmp/bl/' +# 截图尺寸与鼠标移动尺寸比例 +R = 2 + +fileName = PATH + 'enter.jpg' class MouseController: @@ -30,17 +37,24 @@ def rclick(self, x, y): self.m.release(x, y, 2) -fileName = '/Users/lumr/tmp/test.jpg' +def findImgLocation(srcPath, templatePath): + src = cv2.imread(PATH + srcPath, 0) + temp = cv2.imread(PATH + templatePath, 0) + res = cv2.matchTemplate(src, temp, cv2.TM_CCORR_NORMED) + h, w = temp.shape[0:2] + min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) + return (max_loc[0] + w / 2) / R, (max_loc[1] + h / 2) / R + if __name__ == '__main__': - call(['screencapture', '-tjpg', '/Users/lumr/tmp/test.jpg']) - cv2.imread(fileName) - m = MouseController(pymouse.PyMouse()) - k = pykeyboard.PyKeyboard() - # print('鼠标当前位置是:%s,%s' % m.position()) - m.click(600, 500, 2) - - # m.click(600, 500, 2, 2) - # m.press(720, 450, 2) - time.sleep(0.1) - # m.release(720, 450, 2) + # call(['screencapture', '-tjpg', fileName]) + gScreen = cv2.imread(PATH + 'enter.jpg', 0) + screenHeight, screenWidth = gScreen.shape[0:2] + pbutton = cv2.imread(PATH + 'enterbutton.jpg', 0) + res = cv2.matchTemplate(gScreen, pbutton, cv2.TM_CCORR_NORMED) + min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) + print('max value is {0} pos is {1}'.format(max_val, max_loc)) + x, y = findImgLocation('enter.jpg', 'enterbutton.jpg') + print('x = {0} , y = {1}'.format(x, y)) + m = MouseController(PyMouse()) + m.rclick(x, y) From 3289576963c189a0a7f6b70b7597823520aad433 Mon Sep 17 00:00:00 2001 From: lumr Date: 2019年9月28日 20:14:31 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E7=A2=A7=E8=93=9D=E8=88=AA=E7=BA=BF?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=89=A7=E8=A1=8C=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bilanauto.py | 197 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 178 insertions(+), 19 deletions(-) diff --git a/src/bilanauto.py b/src/bilanauto.py index 4ee2e01..eedff9c 100644 --- a/src/bilanauto.py +++ b/src/bilanauto.py @@ -3,20 +3,26 @@ from subprocess import call import cv2 -import pykeyboard +import numpy as np from pymouse import PyMouse # 图片路径 PATH = '/Users/lumr/tmp/bl/' # 截图尺寸与鼠标移动尺寸比例 R = 2 +# 图片相似度 +DIFF = 0.985 -fileName = PATH + 'enter.jpg' +TMPScreenPath = PATH + 'tmp.jpg' class MouseController: - def __init__(self, mouse): - self.m = mouse + """ + 鼠标控制器 + """ + + def __init__(self): + self.m = PyMouse() def position(self): print('鼠标当前位置是:%s,%s' % self.m.position()) @@ -28,33 +34,186 @@ def click(self, x, y, button=1, times=1): self.lclick(x, y, times) self.position() + def move(self, x, y): + self.m.move(x, y) + print('mouse move to (%d,%d)' % (x, y)) + def lclick(self, x, y, times=1): self.m.click(x, y, 1, times) + print('mouse lclick on (%d,%d)' % (x, y)) def rclick(self, x, y): self.m.press(x, y, 2) time.sleep(0.1) self.m.release(x, y, 2) + print('mouse rclick on (%d,%d)' % (x, y)) + + +NODES = sorted(['0', + '1-0', '1-1', '1-1', + # '2-0', '2-1', + '3-0', '3-1', '3-2', '3-3', '3-4' + ]) + + +class ProcessManager: + """游戏流程控制器 + @:param status 0 未开始 1开始 2出错 + @:param node 游戏处于的环节:0未开始 1关口选择 2确认进入关口 3小怪清理 4Boss清理 5结束 + 3和4细分:1选择怪物 2点击怪物 3开始战斗 4战斗结束 5确认奖励 6结束 + """ + + def __init__(self): + self.status = 0 + self.node = 0 + self.kill_boss = False + self.exchange_time = 0 # 切换船次数 + self.ship_time = 5 # 第1只船剩余油数 + self.other_ship_time = 5 + self.m = MouseController() + + def start(self, start_node='1-0'): + """ + 开始脚本 + :return: + """ + print('脚本开始。') + self.status = 1 + self.node = NODES.index(start_node) + while True: + time.sleep(3) + if self.node>= len(NODES): + self.node = 1 + break + if self.status == 1: + self.next() + elif self.status == 2: + break + print('脚本结束。') + + def next(self): + """ + 进入下一个环节 + :return: + """ + node_name = NODES[self.node] + print('进入%s环节' % node_name) + if node_name == '3-0': # 寻敌环节 + time.sleep(3) + if self.ship_time <= 0: + self.click_image('exchange') + self.ship_time,self.other_ship_time = self.other_ship_time,self.ship_time + time.sleep(2) + self.exchange_time += 1 + if exist_image('boss')[0]: + if self.exchange_time % 2 != 1: + self.click_image('exchange') + self.ship_time, self.other_ship_time = self.other_ship_time, self.ship_time + return + self.click_image('boss') + self.kill_boss = True + elif exist_image('level-mid')[0]: + self.click_image('level-mid', 50, 50) + elif exist_image('level-hard')[0]: + self.click_image('level-hard', 50, 50) + elif exist_image('ship-easy')[0]: + self.click_image('ship-easy') + elif exist_image('ship-mid')[0]: + self.click_image('ship-mid') + elif exist_image('item'): + self.click_image('item', 0, 50) + time.sleep(3) + if exist_image('3-1')[0]: + self.click_image('3-3') + return + else: + return + self.node += 1 + elif node_name == '3-1': + time.sleep(5) + if exist_image('skip')[0]: + self.click_image('skip') + self.node = NODES.index('3-0') + else: + self.click_image('3-1') + self.node += 1 + elif node_name == '3-2': + time.sleep(60) + while not exist_image('3-2')[0]: + time.sleep(10) + self.click_image('3-2') + self.node += 1 + elif node_name == '3-4': # 战斗结束 + self.click_image('3-4') + if self.kill_boss: + self.node += 1 + self.kill_boss = False + else: + self.node = NODES.index('3-0') + self.ship_time -= 1 + else: + if self.click_image(node_name): + self.node += 1 + def click_image(self, tar_image, w=0, h=0, mindiff=DIFF): + """ + 点击图片进入下一步 + :param tar_image: + :return: + """ + for i in range(10): + print('点击%s 按钮,第%d次尝试' % (tar_image, i)) + screen() + time.sleep(0.2) + diff, x, y = findImgLocation(tar_image + '.jpg') + if diff> mindiff: + self.m.lclick(x + w, y + h) + return True + self.status = 2 + return False -def findImgLocation(srcPath, templatePath): - src = cv2.imread(PATH + srcPath, 0) - temp = cv2.imread(PATH + templatePath, 0) + def __getattr__(self, item): + return self[item] + + +def findImgLocation(templatepath, srcPath=TMPScreenPath): + src = cv2.imread(srcPath, cv2.IMREAD_GRAYSCALE) + temp = cv2.imread(PATH + templatepath, cv2.IMREAD_GRAYSCALE) + # src_gray = cv2.cvtColor(src, cv2.COLOR_GRAY2BGR) res = cv2.matchTemplate(src, temp, cv2.TM_CCORR_NORMED) h, w = temp.shape[0:2] min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) - return (max_loc[0] + w / 2) / R, (max_loc[1] + h / 2) / R + # cv2.rectangle(src, max_loc, (max_loc[0] + w, max_loc[1] + h), (7, 249, 151), 2) + # cv2.imshow('View', src) + # cv2.waitKey(12000) + # cv2.destroyAllWindows() + print('图片匹配完成,最高匹配率{0},位置为:{1}'.format(max_val, max_loc)) + return max_val, (int(max_loc[0]) + (w>> 1))>> 1, (int(max_loc[1]) + (h>> 1))>> 1 + + +def exist_image(tar_image, minDiff=DIFF): + screen() + time.sleep(0.5) + max_val, x, y = findImgLocation(tar_image + '.jpg') + if max_val> minDiff: + return True, x, y + else: + return False, 0, 0 + + +last_time = 0 + + +def screen(): + global last_time + now = time.time() + if now - last_time> 1.5: + call(['screencapture', '-tjpg', TMPScreenPath]) + last_time = now if __name__ == '__main__': - # call(['screencapture', '-tjpg', fileName]) - gScreen = cv2.imread(PATH + 'enter.jpg', 0) - screenHeight, screenWidth = gScreen.shape[0:2] - pbutton = cv2.imread(PATH + 'enterbutton.jpg', 0) - res = cv2.matchTemplate(gScreen, pbutton, cv2.TM_CCORR_NORMED) - min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) - print('max value is {0} pos is {1}'.format(max_val, max_loc)) - x, y = findImgLocation('enter.jpg', 'enterbutton.jpg') - print('x = {0} , y = {1}'.format(x, y)) - m = MouseController(PyMouse()) - m.rclick(x, y) + pm = ProcessManager() + print('脚本即将开始,请3秒内将画面移动到模拟器。') + time.sleep(3) + pm.start('3-0')

AltStyle によって変換されたページ (->オリジナル) /