crac

导航

python常用

获取当前时间

def getNowTime():
	currenttime=datetime.datetime.now()
	res=currenttime.strftime("%Y_%m_%d")
	return res

时间戳转换:

def datetime_timestamp(dt):

	dt=dt.replace('年','-').replace('月','-').replace('日','')
	if len(re.findall('^\d+-\d+-\d+$', dt)) == 0 :
		dt = '2019-05-19'
	else:
		dt=dt
	# 中间过程,一般都需要将字符串转化为时间数组
	###time.strptime(dt, '%Y-%m-%d %H:%M:%S')
	time.strptime(dt, '%Y-%m-%d')
	## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=-1)
	# 将"2012-03-28 06:53:40"转化为时间戳
	###s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
	s = time.mktime(time.strptime(dt, '%Y-%m-%d'))
	return int(s)

正则:

def getdate(html):
	reg = (r'<div id="date">(.*?)<b>')
	listre = re.compile(reg)
	mylist = re.findall(listre, html)
	mylist = mylist[0]
	return mylist

常用引用:

# coding=utf-8
import time
from selenium import webdriver
import sys
import ssl
import urllib2
import re
from PIL import Image
import cStringIO
import requests
reload(sys)
sys.setdefaultencoding("utf-8")
ssl._create_default_https_context = ssl._create_unverified_context
requests.packages.urllib3.disable_warnings()

验证码识别获取:

def getvcode():
    burp0_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0; Waterfox) Gecko/20100101 Firefox/56.2.5", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Upgrade-Insecure-Requests": "1"}

    url = 'http://127.0.0.1:7779/api'
    #imgurl='http://=%s' % username
    imgurl ='http://127.0.0.1/vcode_test/vcode.png'
    # imgtxt2=urllib2.urlopen(imgurl).read()
    #print 'code name is :', username
    #print imgurl
    request1 = urllib2.Request(imgurl,headers=burp0_headers)

    response = urllib2.urlopen(request1)
    imgtxt2 = response.read()
    # print imgtxt2
    imgtxt = cStringIO.StringIO(imgtxt2)
    postdata = imgtxt
    html = requests.post(url, data=postdata, timeout=20)
    mynum = (html.text)
    #print mynum
    #mynum= html.text.replace('S','5').replace('L','1').replace('I','1').replace('T','7').replace('X','4').replace('F','1').replace('Z','2')
    with open('Pic\\{}.{}'.format(str(mynum)+'-', 'jpg'), 'wb') as f:
        f.write(str(imgtxt2))

    return mynum

  

本地验证码获读取:

def getvcode_local(str,r,h):

    url='http://127.0.0.1:7779/api'
    img=Image.open('vcode.png')
    imgtxt2=img.resize((int(r),int(h)),Image.BILINEAR)
    print imgtxt2
    imgtxt = cStringIO.StringIO(imgtxt2)
    postdata=imgtxt
    html=requests.post(url,data=postdata,timeout=5)
    mynum=(html.text)
    print mynum
    #mynum= html.text.replace('S','5').replace('L','1').replace('I','1').replace('T','7').replace('X','4').replace('F','1').replace('Z','2')
 

base64数据转图片:

# -*- coding: utf-8 -*-
 
import re
import base64
from cStringIO import StringIO
 
from PIL import Image
 
 
def base64_to_image(base64_str, image_path=None):
    base64_data = re.sub('^data:image/.+;base64,', '', base64_str)
    binary_data = base64.b64decode(base64_data)
    img_data = StringIO(binary_data)
    img = Image.open(img_data)
    if image_path:
        img.save(image_path)
    return img

打印表格:

#coding:utf8
import prettytable as pt
 
def main():
	tb = pt.PrettyTable()
	tb.field_names = ["用户名","时间","URI","地址","文件名"]
	for i in range(1,10,1):
		tb.add_row([i,'time','uri','addres','filename'])
	print tb
 
if __name__ == '__main__':
	main()

HTML转换为PDF:

def validateTitle(title):
    """ 将 title 名字 规则化
    :param title: title name 字符串
    :return: 文件命名支持的字符串
    """
    rstr = r"[\=\(\)\,\/\\\:\*\?\"\<\>\|\' ']"  # '= ( ) , / \ : * ? " < > |  '   还有空格
    new_title = re.sub(rstr, "_", title)  # 替换为下划线
    return new_title
# options = {
#     'animation': 'false', #导出PDF一定要设置,否则显示不全
#
# }
options = {
        'page-size': 'A4',
        'margin-top': '0mm',
        'margin-right': '0mm',
        'margin-bottom': '0mm',
        'margin-left': '0mm',
        # 'orientation':'Landscape',#横向
        'encoding': "UTF-8",
        'no-outline': None,
    'animation':'false',
          # 'footer-right':'[page]' 设置页码
    }
 
#session = requests.session()
confg = pdfkit.configuration(wkhtmltopdf=r'wkhtmltopdf\bin\wkhtmltopdf.exe')
# 这里指定一下wkhtmltopdf的路径
 
readFile = open('url.txt','r',encoding='utf8')
urlList = []
for lines in readFile.readlines():
    urlList.append(lines.strip())
for u in urlList:
    # try:
    print(u)
    name = (get_con(u)[1]+'_'+get_con(u)[0])
 
    print('正在将{}网页转为PDF'.format(name))
    time.sleep(2)
    strCMD = r'wkhtmltopdf\\bin\\wkhtmltopdf.exe --disable-smart-shrinking "%s" %s.pdf' % (u,validateTitle(name))
    print(strCMD)
    os.popen(strCMD)

进制转换:

def str_to_hex(s):
    return r"/x"+r'/x'.join([hex(ord(c)).replace('0x', '') for c in s])

def hex_to_str(s):
    return ''.join([chr(i) for i in [int(b, 16) for b in s.split(r'/x')[1:]]])
    
def str_to_bin(s):
    return ' '.join([bin(ord(c)).replace('0b', '') for c in s])
    
def bin_to_str(s):
    return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]])

  

获取当前文件名:

#coding:utf8
import requests
import os
import sys
#打印绝对目录 print sys.argv print sys.argv[0]
#打印当前文件名 print os.path.basename(sys.argv[0]) print os.path.basename(__file__)
#打印绝对目录 print os.getcwd()

  

 

posted on 2020-07-31 16:17  crac  阅读(135)  评论(0编辑  收藏  举报