封装,调用函数,以及参数化

#登录ecshop,新增地址,删除地址
import requests
import re
s=requests.session()
headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"}
def login(url,name,passwd):
data={
"username":name,
"password":passwd,
"act":"act_login",
"back_act":"./index.php"
}
a=s.post(url,data=data,headers=headers,verify=False)
print('登录code:%s'%a.status_code)
return '登录code:%s'%a.status_code
#print(dir(s))
def addaddr(addressee,address,tel):
addrdata={"country":1,"province":13,"city":135,"district":1475,
"consignee":addressee,"email":"8998@qq.com","address":address,
"tel":tel,"submit":"新增收货地址","act":"act_edit_address"}
ap=s.post(url,data=addrdata,headers=headers)
print('新增地址code:%s'%ap.status_code)
return '新增地址code:%s'%ap.status_code
def deladdr():
idhtml=s.get("http://localhost/ECshop_v3.6.0/ecshop/user.php?act=address_list")
id=re.findall(r"act=drop_consignee&id=(.+?)'",idhtml.text)
idid=id[0]
#print(idid)
addrurl="http://localhost/ECshop_v3.6.0/ecshop/user.php?act=drop_consignee&id="+str(idid)
dd=s.get(addrurl,headers=headers)
print('删除地址code:%s'%dd)
return dd.status_code

url='http://localhost/ECshop_v3.6.0/ecshop/user.php'
names='fuhui'
passwds='P@ssw0rd'
login(url,names,passwds)
addaddr('angel','任汪勾小卖部','15527721040')
deladdr()
上一篇:pselect 函数


下一篇:活动安排-贪心算法-可视化