使用 Python 发送的 WhatsApp 消息中的换行符 您可以使用任何对您更有效的合适方式也许以下方法也适用于您更新:必须使用以下方式或者可以尝试使用 f 或 .format 字符串来简化

问题描述

我正在编写一个相当简单的代码来通过 Python 发送 WhatsApp 消息,我需要使用换行符。

例如,如果消息是“亲爱的学生,请发送您的报告,谢谢您的关注”

WhatsApp 上的消息应该是这样的

亲爱的同学:

请发送您的报告

感谢您的关注

尝试使用 \n 无效。文本必须在单个消息中。下面是我已经进阶的代码,先谢谢大家的帮助。

# Necessary libraries 

import pyautogui as pg
import webbrowser as web
import time    

message = "Dear Student,\n Please send your report\n Thank you for your attention"

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335,690) # click on the submit button location

解决方法

消息使用urlencodedtext,因为是http请求, 因此,您的信息将是

Dear%20Student%2C%0APlease%20send%20your%20report%0AThank%20you%20for%20your%20attention

Check this question first

,
  • 我也遇到了同样的问题,
  • 在 WhatsApp 网络中,“\n”用作密钥。
  • 要在 WhatsApp 网页中换行,您必须使用其快捷键来换行。
  • WhatsApp 网页中换行的快捷键是:SHIFT+ENTER
  • 但是如果我们将它与代码一起使用,那么它将在整个执行过程中保持“SHIFT”键。因此,文本大小写将发生变化。
  • 为了防止大小写更改,您必须再次发送“SHIFT”键以释放“SHIFT”键。 所以要通过代码发送的键是:SHIFT+ENTER+SHIFT

您可以使用任何对您更有效的合适方式。

# Necessary libraries 

import pyautogui as pg
import webbrowser as web
import time
from selenium.webdriver.common.keys import Keys   
#You have to install selenium for keys or can choose any other library.

message = "Dear Student," + (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT) + "Please send your report" + (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT) + "Thank you for your attention"

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335,690) # click on the submit button location

也许以下方法也适用于您。

# Necessary libraries 

import pyautogui as pg
import webbrowser as web
import time    

message = """Dear Student,\
Please send your report          \
Thank you for your attention"""

# In the above just use '\' which is used for concatenation.
# But you have to give extra spaces as I have mentioned.
# because '\' will add only 2 tab spaces in the message during writing.

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335,690) # click on the submit button location

更新:必须使用以下方式

  • 上面的消息看起来太复杂了,无法输入。
  • 因此,您可以通过将变量中的键分配给简单地使用来以以下简单的方式使用它

import pyautogui as pg
import webbrowser as web
import time
from selenium.webdriver.common.keys import Keys   
#You have to install selenium for keys or can choose any other library which be suitable for you.

br = (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT)
message = "Dear Student," + br + "Please send your report" + br + "Thank you for your attention"

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335,690) # click on the submit button location


或者可以尝试使用 f 或 .format 字符串来简化。


import pyautogui as pg
import webbrowser as web
import time
from selenium.webdriver.common.keys import Keys   
#You have to install selenium for keys or can choose any other library which be suitable for you.

br = (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT)
message = f"Dear Student,{br}Please send your report{br}Thank you for your attention"
####################### Or #########################
# message = "Dear Student,{0}Please send your report{0}Thank you for your attention".format(br)

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335,690) # click on the submit button location