使用Python发送的WhatsApp消息中的换行符

2024-05-13 07:29:53 发布

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

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

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

WhatsApp上的消息应该是这样的

Dear Student,

Please send your report

Thank you for your attention

使用\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

Tags: 代码importreportyousendweb消息for
2条回答

对消息使用urlencodedtext,因为它是http请求, 因此,你的信息是

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

Check this question first

  • 我也有同样的问题,
  • 在WhatsApp web中,“\n”用作键
  • 要在WhatsApp网站中断开一条线,你必须使用它的短键来断开这条线
  • WhatsApp web中换行的短键为: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


相关问题 更多 >