python-mechanize/mechanize

Zip File Upload As part of Online Form

dexman545 opened this issue · 4 comments

Attempting to upload a zip file as part of a form with other selections. Having tried various inputs to br.form.add_file(), python still spits out an error. Also unsure if I need to provide the name/id of the upload portion of the form.

Tried loading the zip with ZipFile and passing that directly to the upload function, also tried providing MIME type and upload file name.

`br.select_form(id='upload_form')
print('Supplying Text...')
br['chapter_name'] = 'test'
br['chapter_number'] = '55'
zip = zips / '16x16.zip'
#with ZipFile(zip, 'r') as zip:
br.form.add_file(open(zip))
print('Submitting form...')
response = br.submit()

Current setup provides this error: 'charmap' codec can't decode byte 0x9d in position 199: character maps to `

Python 3.7
Mechanize 0.42

It would help if you posted the full error, including the traceback and
also what webpage you are connecting to so I can try to reproduce.

I am unsure of what changed, but I no longer get an error when trying to upload, though the upload is still unsuccessful.
Attempting to upload here: https://mangadex.org/upload/47 (a dummy manga for testing purposes), if successful should have a chapter appear here: https://mangadex.org/title/47

Code:
`import re
import mechanize
from pathlib import Path
from zipfile import ZipFile
import requests

br = mechanize.Browser()

br.set_handle_robots( False )
br.addheaders = [('User-agent', 'Firefox')]
print('Logging in...')

br.open('https://mangadex.org/login')
br.select_form(id="login_form")
br['login_username'] = ''
br['login_password'] = '
****'
br.submit()
print('Logged in')

zips = Path('ProcessData/zips')
rawsB = Path('ProcessData')
rawsB.mkdir(parents=True, exist_ok=True)

m = rawsB / 'debug.txt'

try:
response = br.open('https://mangadex.org/upload/47')
br.select_form(id='upload_form')
print('Supplying Text...')
br.set_all_readonly(False)
br['chapter_name'] = 'testy'
br['chapter_number'] = '33'
zip = zips / 'test.zip'
#with ZipFile(zip, 'r') as zip:
br.form.add_file(open(zip, 'rb'))
print('Submitting form...')
r = br.submit()
m.write_text(str(r.read()))
except mechanize.HTTPError as e:
print(e)
print(e.code)
`

sorry posted on wrong issue

Hard for me to say, since I dont have access to that server, but try setting the content tye and filename by using the content_type and filename arguments to add_file()

Probably the server needs one or both of those to process the file upload.