我正在编写一个相当简单的代码来通过 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
# 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
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
可以在以下链接中阅读我的另一个答案,了解有关使用 python 的 WhatsApp 网络的更多信息。
我正在使用 python 开发 WhatsApp 机器人。
如需捐款,您可以联系:[电子邮件受保护]
请在我的https://github.com/4NUR46如果这个答案对您有帮助。
你也可以使用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])