使用 python 机械化上传文件
Uploading file using python mechanize
我在 windows 10 笔记本电脑上。
当我在本地计算机上手动打开 submit.html 单击并浏览到 namo.jpg namo.png 然后提交时,我得到网站处理我的图像和 return 在 15 秒内生成结果文件。
但是我似乎无法使用 Python 机械化来让它做同样的事情,当它 运行 脚本时, mechanize_results.html 文件保持 returning 太快并告诉我在他们的页面中 "Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.. "
不确定我必须更改什么才能让网站将我的 python 机械化脚本提交的文件识别为图像文件。
我的 submit.html 文件有这个
<form name="myform" id="myform" action="http://deepdreamgenerator.com/upload-im" enctype="multipart/form-data" method="POST" id="upload-form">
<input type="hidden" name="_token" value="pfC1a6HGVdbWO7mCmKVkqVinCkSYOKkQxXZV9NY1">
<input type="file" name="file" id="upload"/>
<input type="submit" />
</form>
我的 python 机械化脚本有这个
进口机械化
filename = 'C:/Users/tintran/Desktop/namo.png'
url = "file:///C:/Users/tintran/Desktop/submit.html"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url)
br.select_form('myform')
br.set_all_readonly(False)
br.form.add_file(open(filename,'r'))
res = br.submit()
content = res.read()
with open("mechanize_results.html", "w") as f:
f.write(content)
https://docs.python.org/2/library/functions.html#open
If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.
这里都是关于 Windows 的。所以只需使用 'rb' 打开 PNG 文件。
我在 windows 10 笔记本电脑上。
当我在本地计算机上手动打开 submit.html 单击并浏览到 namo.jpg namo.png 然后提交时,我得到网站处理我的图像和 return 在 15 秒内生成结果文件。
但是我似乎无法使用 Python 机械化来让它做同样的事情,当它 运行 脚本时, mechanize_results.html 文件保持 returning 太快并告诉我在他们的页面中 "Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.. "
不确定我必须更改什么才能让网站将我的 python 机械化脚本提交的文件识别为图像文件。
我的 submit.html 文件有这个
<form name="myform" id="myform" action="http://deepdreamgenerator.com/upload-im" enctype="multipart/form-data" method="POST" id="upload-form">
<input type="hidden" name="_token" value="pfC1a6HGVdbWO7mCmKVkqVinCkSYOKkQxXZV9NY1">
<input type="file" name="file" id="upload"/>
<input type="submit" />
</form>
我的 python 机械化脚本有这个 进口机械化
filename = 'C:/Users/tintran/Desktop/namo.png'
url = "file:///C:/Users/tintran/Desktop/submit.html"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url)
br.select_form('myform')
br.set_all_readonly(False)
br.form.add_file(open(filename,'r'))
res = br.submit()
content = res.read()
with open("mechanize_results.html", "w") as f:
f.write(content)
https://docs.python.org/2/library/functions.html#open
If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.
这里都是关于 Windows 的。所以只需使用 'rb' 打开 PNG 文件。