Skip to main content
 首页 » 编程设计

python之使用 urllib.request tinyupload.com 上传文件

2025年05月04日14dudu

我想写一个程序在http://www.tinyupload.com/上上传文件,所以我搜索了一种上传表单的方法。 我写了这段代码来上传文件:

import urllib.request 
import urllib.parse 
import http.cookiejar 
import re 
 
# Use cookies 
cookie = http.cookiejar.CookieJar() 
urllib.request.install_opener(urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))) 
 
# A function to download an url (GET) 
def urldownload(url): 
    try: 
        page = urllib.request.urlopen(url) 
        return page.read().decode('iso-8859-2') 
    except urllib.error.HTTPError: 
        return False 
 
# Get the url from the form to upload 
def geturl(html): 
        regex = re.compile('\<form action="(.*?)\" name="upload_form"', re.S) 
        url = regex.findall(html)[0] 
        return(str(url)) 
 
# Get the sid from the url 
def getsessionid(url): 
        return url[-26:] 
 
# Upload a file 
def upload(file): 
    url = geturl(urldownload('http://s000.tinyupload.com/index.php')) 
    sessionid = getsessionid(url) 
    f = open(file).read() 
    data = {'MAX_FILE_SIZE': '52428800', 
            'uploaded_file': f, 
            'file_description': 'File: %s' % (file), 
            'sessionid': sessionid} 
    data = urllib.parse.urlencode(data) 
    result = urllib.request.urlopen(url, data.encode('iso-8859-2')) 
    return(result.read().decode('iso-8859-2')) 
    #return(str(result.info())) 

我应该去下载文件链接的页面,但我有表格。怎么了??

请您参考如下方法:

上传文件有更简单的方法——使用requests图书馆。

import requests 
 
session = requests.Session() 
 
index_url   = 'http://s000.tinyupload.com/index.php' 
upload_url  = 'http://s000.tinyupload.com/cgi-bin/upload.cgi?sid=' 
 
index_request = session.get(index_url) 
PHPSESSID = index_request.cookies['PHPSESSID'] 
 
files = {'file': open('bitcoin.pdf', 'rb')} 
r = requests.post(upload_url+PHPSESSID, files=files) 
 
#Print "File upload finished" page 
print r.text 
 
#Print download link 
import re 
print re.search('http://s000\.tinyupload.com/\?file_id=[^<]+',r.text).group(0)