python模拟登录ecshop并发布商品
import requestsimport logging
import base64
import time
流程
会用到requests.session()1. 打开登录页
2. 模拟post登录
3. 获取登录后页面
4. 准备图片
5. 准备商品数据
6. 模拟post发布商品
代码
#coding:utf-8import requests
import logging
import base64
import time
baseUrl = "http://www.ecshop.com:600"
def login():
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Host": "www.ecshop.com",
"If-Modified-Since": "Thu, 02 Apr 2020 11:41:29 GMT",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
}
#0. 初始化
ss = requests.session()
r = ss.get("{0}/admin/privilege.php".format(baseUrl), headers=headers)
#1. 登录
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Length": "43",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "www.ecshop.com",
"Origin": "{0}",
"Pragma": "no-cache",
"Referer": "{0}/admin/privilege.php?act=login",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
}
data = {
"username": "admin",
"password": "admin123",
"remember": "1",
"act": "signin",
}
r = ss.post("{0}/admin/privilege.php".format(baseUrl), data=data, headers=headers)
logging.warning(r.status_code)
with open("tmp/privilege.html", "wb") as f:
f.write(r.content)
#2. 打开后台
r = ss.get("{0}/admin/index.php?act=menu".format(baseUrl))
with open("tmp/menu.html", "wb") as f:
f.write(r.content)
r = ss.get("{0}/admin/index.php?act=drag".format(baseUrl))
with open("tmp/drag.html", "wb") as f:
f.write(r.content)
r = ss.get("{0}/admin/index.php?act=top".format(baseUrl))
with open("tmp/top.html", "wb") as f:
f.write(r.content)
r = ss.get("{0}/admin/index.php?act=main".format(baseUrl))
with open("tmp/main.html", "wb") as f:
f.write(r.content)
return ss
def post_img(ss, pic_path):
f = {
"localUrl": (None,"1.jpg"),
"file": ("1.jpg", open(pic_path, "rb"), "image/jpeg")
}
r = ss.post("{0}/admin/goods.php?act=ajax_upload_image".format(baseUrl), files=f)
rt = r.json()
img_id = rt["content"]["img_id"]
img_id = str(img_id)
logging.info(rt)
logging.info(img_id)
return img_id
def post_item(ss):
#0. 商品相册准备
p1 = post_img(ss, "1.jpg")
p2 = post_img(ss, "2.jpg")
p3 = post_img(ss, "3.jpg")
p4 = post_img(ss, "4.jpg")
img_sn = ",".join([p1, p2, p3, p4])
#1. 准备商品图片 及 缩率图
f = {
"localUrl": (None,"1.jpg"),
"file": ("1.jpg", open("1.jpg", "rb"), "image/jpeg"),
"goods_img": ("2.jpg", open("2.jpg", "rb"), "image/jpeg"),
"goods_thumbs": ("3.jpg", open("3.jpg", "rb"), "image/jpeg"),
}
#2. 准备商品基本信息
data = {
"goods_name": "{0}我是标题【python发布】".format(time.strftime("%H:%M:%S")),
"shop_price": "600.00",
"cat_id": "27",
"cat_id1": "352",
"cat_id2": "352",
"goods_desc": "<p>我是描述【python发布】</p>",
"is_on_sale": "1",
"img_sn": img_sn,
"MAX_FILE_SIZE": "2097152",
"promote_start_date": "2020-04-02",
"promote_end_date": "2020-05-02",
"goods_name_color": "",
"goods_name_style": "",
"goods_sn": "",
"addedCategoryName": "",
"brand_id": "0",
"addedBrandName": "",
"suppliers_id": "0",
"user_price[]": "-1",
"user_rank[]": "34",
"user_price[]": "-1",
"user_rank[]": "23",
"user_price[]": "-1",
"user_rank[]": "35",
"volume_number[]": "",
"volume_price[]": "",
"market_price": "0",
"virtual_sales": "0",
"give_integral": "-1",
"rank_integral": "-1",
"integral": "0",
"goods_img_url": "",
"goods_thumb_url": "",
"auto_thumb": "1",
"goods_weight": "",
"weight_unit": "1",
"goods_number": "1",
"warn_number": "1",
"is_alone_sale": "1",
"keywords": "",
"goods_brief": "",
"seller_note": "",
"goods_type": "0",
"brand_id1": "0",
"keyword1": "",
"is_single": "1",
"brand_id2": "0",
"keyword2": "",
"price2": "",
"article_title": "",
"goods_id": "0",
"act": "insert",
}
r = ss.post("{0}/admin/goods.php?act=add".format(baseUrl), data=data, files = f)
logging.warning(r.status_code)
if __name__ == "__main__":
ss = login()
post_item(ss)
以上是 python模拟登录ecshop并发布商品 的全部内容, 来源链接: utcz.com/z/515058.html