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/README.md b/README.md new file mode 100644 index 0000000..5269fa6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# studyPython +学习python过程中写的代码 diff --git a/src/bilanauto.py b/src/bilanauto.py new file mode 100644 index 0000000..eedff9c --- /dev/null +++ b/src/bilanauto.py @@ -0,0 +1,219 @@ +#!/usr/bin/env python3 +import time +from subprocess import call +import cv2 + +import numpy as np +from pymouse import PyMouse + +# 图片路径 +PATH = '/Users/lumr/tmp/bl/' +# 截图尺寸与鼠标移动尺寸比例 +R = 2 +# 图片相似度 +DIFF = 0.985 + +TMPScreenPath = PATH + 'tmp.jpg' + + +class MouseController: + """ + 鼠标控制器 + """ + + def __init__(self): + self.m = PyMouse() + + 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 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 __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) + # 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__': + pm = ProcessManager() + print('脚本即将开始,请3秒内将画面移动到模拟器。') + time.sleep(3) + pm.start('3-0') diff --git a/src/githubTending.py b/src/githubTending.py new file mode 100644 index 0000000..75d4e72 --- /dev/null +++ b/src/githubTending.py @@ -0,0 +1,33 @@ +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") + 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'}): + 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)) + + +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/lagouSearch.py b/src/lagouSearch.py new file mode 100644 index 0000000..952bf09 --- /dev/null +++ b/src/lagouSearch.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +import csv +import sys +from time import sleep + +import requests +from bs4 import BeautifulSoup, element +from selenium import common, webdriver + + +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='/Users/meicloud/apps/bin/chromedriver', 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(30)): + 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') + 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 + saveResult(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://localhost:9200/works/_doc/%s' % str(num), json=result) + print(resp) + num = num + 1 + + +if __name__ == '__main__': + # ansyLocal() + search() \ No newline at end of file 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/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')) diff --git a/src/urltest.py b/src/urltest.py new file mode 100755 index 0000000..5a5337f --- /dev/null +++ b/src/urltest.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 + +from urllib import request +from bs4 import BeautifulSoup +from selenium import webdriver, common +from time import sleep +import requests +import csv + +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(url): + html = urlopen(url) + bsobj = BeautifulSoup(html, features="lxml") + for link in bsobj.findAll('a'): + if 'href' in link.attrs: + print(link.attrs['href']) + + +def seleniumtest(): + options = webdriver.ChromeOptions() + options.add_experimental_option('prefs', { + 'profile.default_content_settings': { + 'images': 2 + }, + 'profile.managed_default_content_settings': { + '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.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') + 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']) + 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'})

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