【问题标题】:Python mechanize to fill form with variables or extracted textPython 机械化以使用变量或提取的文本填充表单
【发布时间】:2016-08-07 04:58:36
【问题描述】:

我正在尝试使用 python mechanize 制作一个脚本来自动填写表格。值会根据其他因素而变化。有没有办法让它从文件中读取?这是我的代码:

br.open('https://url/url)
br.select_form(nr=0)
br.form['xxxxx']='123456'
br.form['yyyyy']='7890'
br.submit()                                           
print br.response().read()

我怎样才能得到类似的东西

br.form['xxxx']=open(xxx.txt,r)

所以它从 xxx.txt 读取并填写表格.. 似乎无法在网络上找到任何内容...

【问题讨论】:

    标签: python forms submit controls mechanize


    【解决方案1】:

    如果 txt 文件只有一行,试试这个。

    br.open(url)
    br.select_form(nr=0)
    with open('example.txt', 'r') as f:
        for line in f:
            br.form['xxxxx']= line
    br.form['yyyyy']='7890'
    response = br.submit()
    search = response.read()
    

    另一方面:

    br.open(url)
    br.select_form(nr=0)
    lines = [line.rstrip('\n') for line in open('example.txt')]
    br.form['xxxxx']= lines[0]
    br.form['yyyyy']= lines[1]
    response = br.submit()
    search = response.read()
    

    【讨论】:

      【解决方案2】:
      br.form['xxxx']=open(xxx.txt,r).read()
      

      这将直接给出文件的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多