首页 > 技术文章 > python自动化提交,让女朋友不再熬夜加班

zhangjun0204 2020-03-19 11:42 原文

把图片上传到公司网站后台,然后给图片命名,就完事了。
几小时的工作,几分钟搞定。

# -*-coding:utf-8 -*-

import requests
from requests import exceptions
import re
import  time
import os


def getHtml(name,path):
    files = {'image': (name, open(path, 'rb'))}
    #定义请求头 伪装成浏览器 添加 cookie
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',"Cookie": "PHPSESSID=1v0uo************"}
    try:
        r=requests.post('https://www.XXXX.com/admin_picture/',data={'name':name+" wx"},files=files,headers=headers)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except requests.RequestException as e:
        return e.strerror

def getfile():
    for  filenames in os.walk("C:\\Users\\Documents\\WeChat Files\\zj19950204\\FileStorage\\File\\2020-03\\wx\\wx"):  
        for i in filenames:
            #print(i.split('.')[0])
            getHtml(i.split('.')[0],"C:\\Users\\Documents\\WeChat Files\\zj19950204\\FileStorage\\File\\2020-03\\wx\\wx\\"+i)
            #停顿一下,避免服务器崩掉
            time.sleep(2)

getfile()

推荐阅读