diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a05e2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,91 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# 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/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# 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 + +# dotenv +.env + +# virtualenv +.venv/ +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject diff --git a/ASCII-ART/picture_to_pixles.py b/ASCII-ART/picture_to_pixles.py index bad19c2..ce4902e 100644 --- a/ASCII-ART/picture_to_pixles.py +++ b/ASCII-ART/picture_to_pixles.py @@ -1,14 +1,19 @@ # -*- coding=utf-8 -*- from PIL import Image +import sys +import requests +import StringIO -ASCII_CHARS = ['#', '@', '$', '&', 'S', '+', '=', ';', ':', '.', ' '] + +ASCII_CHARS = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft|()1{}[]?-_+~i!lI;:,^ " max_width=100 #调节此项可调节图片大小,越大图片信息越好。 +range_width=float(255)/(len(ASCII_CHARS)-1) def scale_image(image, new_width=max_width): """Resizes an image preserving the aspect ratio. """ (original_width, original_height) = image.size - aspect_ratio = original_height/float(original_width) + aspect_ratio = original_height/float(original_width)*0.5 new_height = int(aspect_ratio * new_width) new_image = image.resize((new_width, new_height)) return new_image @@ -16,14 +21,13 @@ def scale_image(image, new_width=max_width): def convert_to_grayscale(image): return image.convert('L') -def map_pixels_to_ascii_chars(image, range_width=25): +def map_pixels_to_ascii_chars(image, range_width=range_width): """Maps each pixel to an ascii char based on the range in which it lies. 0-255 is divided into 11 ranges of 25 pixels each. """ pixels_in_image = list(image.getdata()) - pixels_to_chars = [ASCII_CHARS[pixel_value/range_width] for pixel_value in - pixels_in_image] + pixels_to_chars = [ASCII_CHARS[int(pixel_value/range_width)] for pixel_value in pixels_in_image] return "".join(pixels_to_chars) def convert_image_to_ascii(image, new_width=max_width): @@ -39,8 +43,15 @@ def convert_image_to_ascii(image, new_width=max_width): def handle_image_conversion(image_filepath): image = None + session=requests.Session() + session.headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'} try: - image = Image.open(image_filepath) + if image_filepath.startswith('http://') or image_filepath.startswith('https://'): + resp=session.get(image_filepath) + imagebuf=StringIO.StringIO(resp.content) + image=Image.open(imagebuf) + else: + image = Image.open(image_filepath) except Exception, e: print "Unable to open image file {image_filepath}.".format(image_filepath=image_filepath) print e @@ -49,7 +60,10 @@ def handle_image_conversion(image_filepath): print image_ascii if __name__=='__main__': - image_file_path = 'xzz.jpg' - handle_image_conversion(image_file_path) - - + while 1: + print u"请输入图片地址:" + try: + image_file_path = sys.argv[1] + except: + image_file_path=raw_input() + handle_image_conversion(image_file_path) diff --git a/README.md b/README.md index e8dbad1..647b8c8 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,13 @@ 就是一些~~有用的~~Python脚本 1. hostloc:hostloc访问空间获取金币,每天运行一次,每次20金币 -2. zhihu:知乎图片下载器,下载某个问答下面所有的图片 +2. zhihu:知乎图片下载器,下载某个问答下面所有的图片(接口更新,目前已经失效) 3. tumblr:Tumblr爬虫 4. v2ex:v2ex签到脚本 5. fuliba:福利吧签到脚本 6. ASCII-ART:图片转字符画 +7. baidu:百度图床~~ +8. alipay: 支付宝登录&获取订单信息 > 注: diff --git a/alipay/README.md b/alipay/README.md new file mode 100644 index 0000000..604313d --- /dev/null +++ b/alipay/README.md @@ -0,0 +1,11 @@ +# 支付宝登录 & 获取订单信息脚本 + +使用前准备: +1. 安装selenium:`pip install selenium` +2. 下载webdriver。phantomjs无界面,适合linux;chromedriver方便调试。自行百度下载 +3. 修改**USERNMAE**和**PASSWD** + +运行: +`python alipay_login.py` + +pps. 脚本非原创。在v2ex一名v友的基础上修改的 diff --git a/alipay/alipay.py b/alipay/alipay.py new file mode 100644 index 0000000..78b9fa2 --- /dev/null +++ b/alipay/alipay.py @@ -0,0 +1,181 @@ +#-*- coding=utf-8 -*- +""" +支付宝登录获取订单信息脚本 +ps.没啥卵用,使用selenium不稳定,经常出现问题。 +使用前准备: +1.安装selenium:pip install selenium +2.下载webdriver。phantomjs无界面,适合linux;chromedriver方便调试。自定百度 +3.修改USERNMAE和PASSWD + +运行: +python alipay_login.py + +pps. 脚本非原创。在v2ex一名v友的基础上修改的 +""" +import requests +from selenium import webdriver +import time +import pickle +import re +requests.packages.urllib3.disable_warnings() + +# 登录 url +Login_Url = 'https://auth.alipay.com/login/index.htm?goto=https://consumeprod.alipay.com/record/advanced.htm' +# 账单 url +Bill_Url = 'https://consumeprod.alipay.com/record/advanced.htm' +# 登录用户名和密码 +USERNMAE = '' +PASSWD = '' +# 自定义 headers +HEADERS = { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36', + 'Referer': 'https://consumeprod.alipay.com/record/advanced.htm', + 'Host': 'consumeprod.alipay.com', + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', + 'Connection': 'keep-alive' +} + + +class Alipay_Bill_Info(object): + '''支付宝账单信息''' + + def __init__(self, headers, user, passwd): + ''' + 类的初始化 + headers:请求头 + cookies: 持久化访问 + info_list: 存储账单信息的列表 + ''' + self.headers = headers + # 初始化用户名和密码 + self.user = user + self.passwd = passwd + # 利用 requests 库构造持久化请求 + self.session = requests.Session() + # 将请求头添加到缓存之中 + self.session.headers = self.headers + try: + cookies = pickle.load(open("cookies", "rb")) + for cookie in cookies: + self.session.cookies.set(cookie['name'], cookie['value']) + print u"获取cookies成功!" + except: + print u"未登陆过,需先登录" + self.get_cookies() + if not self.login_status(): + print u"cookies失效,重新登录" + self.get_cookies() + # 初始化存储列表 + self.info_list = [] + + def wait_input(self, ele, str): + '''减慢账号密码的输入速度''' + for i in str: + ele.send_keys(i) + time.sleep(0.5) + + def get_cookies(self): + '''获取 cookies''' + # 初始化浏览器对象 + # sel = webdriver.PhantomJS( + # executable_path='C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe') + # sel = webdriver.PhantomJS( + # executable_path='/root/phantomjs/bin/phantomjs') + sel = webdriver.Chrome(executable_path='C:/chromedriver.exe') + sel.maximize_window() + sel.get(Login_Url) + sel.implicitly_wait(3) + # 找到用户名字输入框 + uname = sel.find_element_by_id('J-input-user') + uname.clear() + print u"正在输入账号....." + self.wait_input(uname, self.user) + time.sleep(1) + # 找到密码输入框 + upass = sel.find_element_by_id('password_rsainput') + upass.clear() + print u"正在输入密码...." + self.wait_input(upass, self.passwd) + # 截图查看 + sel.save_screenshot('1.png') + # 找到登录按钮 + button = sel.find_element_by_id('J-login-btn') + time.sleep(1) + print 1 + button.click() + print 2 + sel.save_screenshot('2.png') + if len(re.findall('checkSecurity', sel.current_url)) > 0: + riskackcode = sel.find_element_by_id('riskackcode') + riskackcode.clear() + print u"等待输入验证码:" + msgcode = raw_input() + self.wait_input(riskackcode, msgcode) + button = sel.find_element_by_xpath( + '//*[@id="J-submit"]/input') # ui-button + time.sleep(1) + button.click() + sel.save_screenshot('2.1.png') + print(sel.current_url) + # 跳转到账单页面 + print u"正在跳转页面...." + sel.get(Bill_Url) + sel.implicitly_wait(3) + sel.save_screenshot('3.png') + # 获取 cookies 并转换为字典类型 + cookies = sel.get_cookies() + pickle.dump(cookies, open("cookies", "wb")) + for cookie in cookies: + self.session.cookies.set(cookie['name'], cookie['value']) + # 关闭浏览器 + sel.close() + + def set_cookies(self): + '''将获取到的 cookies 加入 session''' + self.get_cookies() + + def login_status(self): + '''判断登录状态''' + # 添加 cookies + status = self.session.get( + Bill_Url, timeout=5, allow_redirects=False, verify=False).status_code + print(status) + if status == 200: + return True + else: + return False + + def get_data(self): + ''' + 利用 正则表达式解析 html + 并抓取数据, + 数据以字典格式保存在列表里 + ''' + status = self.login_status() + if status: + html = self.session.get(Bill_Url, verify=False).text + # 抓取前五个交易记录 + trades = re.findall('
.*?(\d{4}\.\d{2}\.\d{2})', trade)[0] + time = re.findall( + '
.*?(\d{2}:\d{2})', trade)[0] + amount = re.findall( + '(.*?) ', trade)[0] + ddh = re.findall( + '