【问题标题】:WhatsApp texting using webbrowser module of python使用 python 的 webbrowser 模块的 WhatsApp 发短信
【发布时间】:2020-04-21 03:39:05
【问题描述】:
import webbrowser
import time
import datetime

name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
message = input('Enter the message: ')
time1 = input('Enter time in {hh:mm:ss} format: ')
print(f'Time entered by user: {time1}')
while True:
    current_time = time.ctime()
    time_format = current_time[11:19]
    time.sleep(1)
    print(f'Current time: {time_format}')
    if time1 == time_format:
        webbrowser.open_new_tab(f'https://web.whatsapp.com/send?phone=+91{name}&text={message}')
        break
    elif time1 < time_format:
        print('Enter correct time')
        break
    else:
        print('waiting..')

我将消息、联系电话和时间作为用户的输入。每当条件满足时,WhatsApp 就会打开,并显示您之前输入的联系电话和消息。

唯一的问题是,我必须手动点击发送按钮来发送消息。其他一切正常。

有没有办法做到这一点?如果您在不使用 Selenium 的情况下提供解决方案,那就太好了

提前致谢!

【问题讨论】:

    标签: python whatsapp python-datetime python-webbrowser


    【解决方案1】:

    这似乎不是很有效,也不是很好地不断检查用户给出的时间是否与当前时间相同。 schedule 模块对此非常有帮助,它确实有助于使您的代码看起来更干净。我将提供 2 个答案,一个带有 schedule 库,一个没有它。

    请记住,如果用户输入了一个已经过去的时间,这对程序来说并不重要,它只会在用户提供的时间是当前设备时间的下一次发送消息。例如 如果用户输入 13:10:00 并且当前设备时间是 14:00:00 那么消息将在下一次当前时间是 13:10:00 时发送,即 next天

    import os
    import time
    import webbrowser
    from datetime import datetime
    from string import ascii_letters
    
    name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
    message = input('Enter the message:\n')
    time1 = input('Enter time in {hh:mm:ss} format: ')
    
    # Check if there aren't any ':' in the input time
    if ":" not in time1:
        print("Please input a correct time format")
        os._exit(0)
    # Check if there are any letters in the input time
    elif ascii_letters in time1:
        print("Please input a correct time format")
        os._exit(0)
    
    print(f'Time entered by user: {time1}')
    
    # Check every .9 seconds if the current time is the same as the user input time
    while True:
        current_time = datetime.now().strftime("%H:%M:%S")
        print(f'Current time: {current_time}')
    
        if time1 == current_time:
            webbrowser.open_new_tab(f'https://web.whatsapp.com/send?phone=+91{name}&text={message}')
            break
        else:
            time.sleep(0.9)
    

    使用schedule 库,它可能看起来像这样:

    (你可能会这样做)

    import os
    import time
    import schedule
    import webbrowser
    from string import ascii_letters
    
    def send_whatsapp_msg(name, message):
        webbrowser.open_new_tab(f'https://web.whatsapp.com/send?phone=+91{name}&text={message}')
        return schedule.CancelJob  # do this if you want to send this message only once
        # or just exit the program entirely if you don't want to run any more tasks
        # os._exit(0)
    
    
    name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
    message = input('Enter the message:\n')  # Message to be sent
    time1 = input('Enter time in {hh:mm:ss} format: ')  # Time to sent the message
    
    # Check if there aren't any ':' in the input time
    if ":" not in time1:
        print("Please input a correct time format")
        os._exit(0)
    # Check if there are any letters in the input time
    elif ascii_letters in time1:
        print("Please input a correct time format")
        os._exit(0)
    
    print(f'Time entered by user: {time1}')
    
    # Schedule the message to be sent
    schedule.every().day.at(time1).do(send_whatsapp_msg, name, message)
    
    # Wait for the tasks to run and check the time every .9 seconds
    while True:
        schedule.run_pending()
        time.sleep(0.9)
    
    

    为了解决按钮点击问题,我想我找到了一个 "hacky" 解决方案,使用 seleniumpyautogui。在 Windows 上,您只需安装 WhatsApp 应用程序并扫描 QR 码一次,然后此解决方案可能适合您。

    您还必须安装 selenium chrome 网络驱动程序并截取“箭头”按钮以将消息发送到 WhatsApp 应用程序。这里是 janky 解决方案:

    import os
    import time
    import schedule
    import pyautogui
    from string import ascii_letters
    from selenium import webdriver
    from selenium.webdriver.commn.keys import Keys
    
    
    def send_whatsapp_msg(name, message):
        driver = webdriver.Chrome()  # you can user different drivers like 'Firefox()' but you will have to install them first
        # look at https://selenium-python.readthedocs.io/installation.html for more info
    
        driver.get("https://api.whatsapp.com/send?phone=+91{name}&text={message}")
        time.sleep(10)  # Wait for everything to set up, can be assigned lower values
    
        # Click on the 'Open WhatsApp' Prompt Button
        # Much easier to do it with pyautogui since I couldn't make it work with Selenium
        # Look at https://pyautogui.readthedocs.io/en/latest/quickstart.html#screenshot-functions for the '.locateCenterOnScreen' function
        coords = pyautogui.locateCenterOnScreen("open_whatsapp.png")
        pyautogui.click(coords[0], coords[1])  # read the docs on what '.locateCenterOnScreen' returns
    
        time.sleep(15)  # Wait for the WhatsApp dektop app to load up
    
        coords = pyautogui.locateCenterOnScreen("click_send.png")  # coordinates for the 'send' button
        pyautogui.click(coords[0], coords[1])
        # Your message has been sent!
    
        return schedule.CancelJob  # do this if you want to send this message only once
        # or just exit the program entirely if you don't want to run any more tasks
        # os._exit(0)
    
    
    name = input('Enter the contact number of a person you want to send message in WhatsApp: ')
    message = input('Enter the message:\n')  # Message to be sent
    time1 = input('Enter time in {hh:mm:ss} format: ')  # Time to sent the message
    
    # Check if there aren't any ':' in the input time
    if ":" not in time1:
        print("Please input a correct time format")
        os._exit(0)
    # Check if there are any letters in the input time
    elif ascii_letters in time1:
        print("Please input a correct time format")
        os._exit(0)
    
    print(f'Time entered by user: {time1}')
    
    # Schedule the message to be sent
    schedule.every().day.at(time1).do(send_whatsapp_msg, name, message)
    
    # Wait for the tasks to run and check the time every .9 seconds
    while True:
        schedule.run_pending()
        time.sleep(0.9)
    

    click_send.png 看起来像这样:

    open_whatsapp.png 看起来像这样:

    open_whatsapp.png 是网站提示您的“打开 WhatsApp” 按钮的屏幕截图,但我的语言不同,所以我不得不将其编辑掉。

    我也不知道pyautogui 的可靠性如何,但每次我尝试运行它时它都能正常工作,所以我猜它有点工作。

    【讨论】:

    • 好建议,但您没有回答他的问题,您可以将其作为评论提及。
    • 你是对的,我没有意识到这一点,因为我不使用 WhatsApp,所以没有完全得到问题
    • 他在打开浏览器后试图点击一个按钮,他不想使用 selenium。
    • 是的,我正在调查,刚刚注册了一个 WhatsApp 帐户。
    • 看起来在 Selenium 之外没有任何选项。我没有找到任何可以设置为自动发送消息的 url 变量,而且我发现的一个库也使用 Selenium。另一种选择可能是使用 WhatsApp Business API,但您必须将您的帐户设为企业帐户。
    猜你喜欢
    • 1970-01-01
    • 2022-06-21
    • 2017-06-29
    • 2011-03-12
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多