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

问题描述 投票:0回答:3

我正在编写一个相当简单的代码来通过 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
python whatsapp pyautogui
3个回答
2
投票

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

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

先检查这个问题


2
投票
  • 我也有同样的问题
  • 在 WhatsApp 网络中“ “充当钥匙。
  • 要在 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



0
投票

你也可以使用pywhatkit,更简单,省去一些步骤

import pywhatkit as kit
import time

#phones
contacts = ['+-----']


message = "\"⚠️ IMPORTANT ⚠️\n\nToday is friday\""

start_time = (11, 00)  #  11:00 AM

# send
for contact in contacts:
    kit.sendwhatmsg(contact, message, start_time[0], start_time[1])
© www.soinside.com 2019 - 2024. All rights reserved.