python - 使用 Python Mechanize 上传文件

标签 python file forms upload mechanize

当我运行以下脚本时:

from mechanize import Browser
br = Browser()
br.open(url)
br.select_form(name="edit_form")
br['file'] = 'file.txt'
br.submit()

我得到:ValueError: value attribute is readonly

添加时仍然出现同样的错误:

br.form.set_all_readonly(False)

那么,我如何使用 Python Mechanize 与 HTML 表单交互以上传文件?

理查德

最佳答案

这是使用 Mechanize 正确执行此操作的方法:

br.form.add_file(open(filename), 'text/plain', filename)

关于python - 使用 Python Mechanize 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1299855/

相关文章:

python - Pandas :更改具有多级列的数据框中的特定列名

java - Eclipse 找不到文件?

ruby-on-rails - data-remote true 定义回调

javascript - 如何让用户通过点击添加另一个上传输入?

jquery - 使用enter提交时AJAX表单提交

显示派生指标的 Python pandas pivot_table(来自两列)

Python Selenium,如何保存http响应内容?

python - 在 openshift 上将 json 发布到 Flask 时出现 400 错误请求

c - 在 Linux/C 编程中,使用函数 write 也会在缓冲区上移动指标吗?

file - 如何格式化 CSV 文件中的数据,以便可以轻松地将其导入到 R 中?