【问题标题】:Fill web form with text from txt file using Python mechanize使用 Python mechanize 使用 txt 文件中的文本填充 Web 表单
【发布时间】:2015-11-21 12:14:30
【问题描述】:

我正在尝试使用 txt 文件中的文本使用 Python mechanize 填充 Web 表单,但是当填写表单时,所有返回字符/换行符都会丢失。有没有办法解决这个问题?打印到控制台时,它包含返回,所以我有点难过。

def load_message():
the_message = open('ra_message.txt', 'r')
message = the_message.read()
the_message.close()
return message

def message(to_user, subject, the_message):
try:
    br.open(ra_message)
    br.select_form(nr=0)
    br['addrecipient'] = to_user
    br['subject'] = subject
    br['message'] = the_message
    br.submit()
    print('{} succesfully messaged.'.format(to_user))        
    save_line = to_user + ', TRUE\n'
    save_file = open('messaged_users.csv', 'a')
    save_file.write(save_line)
    save_file.close()
except Exception, e:
    print str(e)

def main():
try:
    the_message = load_message()
    login(username, password)
    for name in users:
        message(name, 'Hi {}'.format(name), the_message.format(vote_link))
    print('All users succesfully messaged.')
except Exception, e:
    print str(e)

【问题讨论】:

    标签: python mechanize


    【解决方案1】:

    经过一番挖掘,我在另一个 Stack Overflow 线程中找到了答案 - https://stackoverflow.com/a/9461440/5588987

    CR = \r LF = \n

    而 HTML 表单将换行符作为 CRLF,因此:

    \r\n 成功了!

    【讨论】:

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