Skip to main content
 首页 » 编程设计

python noob 在发送谷歌语音文本时遇到问题

2025年05月04日9zhwl

我正在编写一个简单的小脚本,以便在 Ultra 音乐节早鸟票开始销售时向我发送短信,以便我抢购。当我开始写这篇文章时,我认为 python 将是实现我目标的快速方法。我所做的是收集链接,然后计算它们并确定是否有变化,然后向几个号码发送谷歌语音短信。这是我针对 stackoverflow 运行的代码。

from googlevoice import Voice 
from googlevoice.util import input 
from bs4 import BeautifulSoup, SoupStrainer 
from time import sleep 
import urllib2 
from array import * 
 
#define login details 
email = 'example@gmail.com' 
password = 'password' 
url = 'http://stackoverflow.com/questions' 
 
def send_message(var_text): 
    voice = Voice() 
    voice.login(email, password) 
 
    phoneNumber = array('L',[9998675309, 9998675309])    
    for i in phoneNumber: 
        voice.send_sms(i, var_text) 
 
#init 
soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a')) 
 
link_count = len(soup) 
 
#start the loop 
var = 1 
while var == 1 :  # This constructs an infinite loop 
    soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a')) 
    if link_count != len(soup):  
        string = str('Link Count Changed\n\nSite:\n' + url + '\nPrev:\n' + str(link_count) + '\nNew:\n' + str(len(soup))) 
        send_message(string) 
        print (string) 
        link_count = len(soup) 
        sleep(10) 
        pass 
    else: 
        print('Number of links ('+ str(link_count) + ') has not changed, going to sleep now.')  
        sleep(10) 
        pass 
print "Good bye!" 

这是我不断收到的错误(似乎只有在发送到多个号码时才会发生)

doesn't work array('L',[9998675309, 9998675309]) 
works array('L',[9998675309]) 

错误:

bash-3.2# python gvsendalert.py  
Number of links (195) has not changed, going to sleep now. 
Traceback (most recent call last): 
  File "gvsendalert.py", line 32, in <module> 
    send_message(string) 
  File "gvsendalert.py", line 19, in send_message 
    voice.send_sms(i, var_text) 
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/voice.py", line 151, in send_sms 
    self.__validate_special_page('sms', {'phoneNumber': phoneNumber, 'text': text}) 
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/voice.py", line 225, in __validate_special_page 
    load_and_validate(self.__do_special_page(page, data)) 
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/util.py", line 65, in load_and_validate 
    validate_response(loads(response.read())) 
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlevoice/util.py", line 59, in validate_response 
    raise ValidationError('There was a problem with GV: %s' % response) 
googlevoice.util.ValidationError: There was a problem with GV: {u'data': {u'code': 58}, u'ok': False} 

好的,我已经考虑了你们中的一些人发布的内容并得出了这个结论。对于发送我的谷歌语音号码两次的数字数组,它将发送 2 条消息。如果我把我的 friend 号码放在第二位,它就会崩溃。这可能是因为我的 friend 号码不是谷歌语音号码吗?我已经能够使用 Google Voice 和其他一些 3rd 方 iPhone 应用程序向这个号码发送消息,所以我认为 python 模块会以相同的方式工作。

这是我的第二个修订代码:

def send_message(var_text): 
    voice = Voice() 
    voice.login(email, password) 
 
    phoneNumber = ['myrealgooglenumber', 'myfriendsactualphonenumber']   
    for i in phoneNumber: 
        print( str('sending to: ') + str(i)) 
        voice.send_sms(str(i), str(var_text)) 
        sleep(5) 
 
#init 
soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a')) 
 
link_count = len(soup) 
 
#start the loop 
var = 1 
while var == 1 :  # This constructs an infinite loop 
    soup = BeautifulSoup(urllib2.urlopen(url).read(), parse_only=SoupStrainer('a')) 
    if link_count != len(soup):  
        string = ('Link Count Changed\n\nSite:\n{0}\nPrev:\n{1}\nNew:\n{2}').format(url, link_count, len(soup))      
        send_message(string)         
        link_count = len(soup) 
        print (string) 
        sleep(10) 
        pass 
    else: 
        string = ('Number of links ({0}) has not changed, going to sleep now.').format(str(link_count)) 
        print(string)  
        sleep(10) 
        pass 
print "Good bye!" 

已使用 2 个谷歌语音号码进行测试并且有效。仍然不适用于非谷歌语音号码。

请您参考如下方法:

看起来您正在为电话号码使用整数。

电话号码不是真实号码。

尝试使用字符串:

phoneNumber = ['9998675309', '9998675309'] 

此外,在样式注释上,查看字符串格式:

string = 'Link Count Changed\n\nSite:\n{0}\nPrev:\n{1}\nNew:\n{2}').format(url, link_count, len(soup))