封装,调用函数,以及参数化
Posted canglongdao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装,调用函数,以及参数化相关的知识,希望对你有一定的参考价值。
#登录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()
以上是关于封装,调用函数,以及参数化的主要内容,如果未能解决你的问题,请参考以下文章