【问题标题】:Line breaks in WhatsApp messages sent with Python使用 Python 发送的 WhatsApp 消息中的换行符
【发布时间】:2021-06-05 00:46:19
【问题描述】:

我正在编写一个相当简单的代码来通过 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


【解决方案1】:

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

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

Check this question first

【讨论】:

  • 您可以将其作为选定答案。这对于正在搜索未回答问题的其他人会有所帮助。
【解决方案2】:
  • 我也有同样的问题,
  • 在 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


【讨论】:

    猜你喜欢
    • 2019-10-21
    • 2017-02-04
    • 2018-08-22
    • 1970-01-01
    • 2019-09-19
    • 2013-10-08
    • 1970-01-01
    • 2018-09-01
    • 2017-12-12
    相关资源
    最近更新 更多