python%s在通过POST方法发送时正在丢弃\n

2024-06-16 11:54:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python做一个电子邮件发送项目

当我用一个变量传递post方法中的%s时,它会丢弃所有新行并将其发送到电子邮件,这样我就可以得到所有填充的电子邮件,有人能帮我吗

后处理方法:

r = requests.post('https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1=%s' % (s))

所以,如果s包含一个标题和正文的新闻,在电子邮件中,我会把它连接在一起


Tags: 项目方法keyhttpscom电子邮件withcustom
2条回答

使用format()构建该url。另外,了解如何使用format()使用命名占位符,下面提供的示例将使用这些占位符

s = s.replace('\n', '%0A')  # Replaces \n with urlencoded \n (%0A)
url = 'https://maker.ifttt.com/trigger/Custom Newsfeed/with/key/nYmoaoROeu6Mf2SrBOgUg?value1={value1}'.format(value1=s)
r = requests.post(url)

也可以使用urlencode

>>> import urllib
>>> f = { 'value1' : s}
>>> urllib.urlencode(f)
'value1=cool+event'

如果在Python3上,试试看 'value1={}'.format(s) 或者如果是3.6,试试看 f'value1={s}'

相关问题 更多 >