我正在写一个简单的小脚本,在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 ChangednnSite: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条消息。如果我把我的朋友号码放在第二个,它会打破它。这可能是因为我的朋友号不是谷歌语音号码吗?我已经能够使用谷歌语音和其他一些第三方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 ChangednnSite: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个谷歌语音号码,它工作。仍然无法使用非谷歌语音号码。
看起来你在用int表示电话号码。
电话号码是而不是真实号码。
改为尝试字符串:
phoneNumber = ['9998675309', '9998675309']
此外,在风格注释中,请查看字符串格式:
string = 'Link Count ChangednnSite:n{0}nPrev:n{1}nNew:n{2}').format(url, link_count, len(soup))
谷歌可能有一个计时器,可以防止你背靠背发送太多短信。
也许你可以试着把你的循环改成这样:
for i in phoneNumber:
voice.send_sms(i, var_text)
sleep(5)
另一个想法是,如果你使用两个不同的电话号码,效果会更好吗?