我有一个脚本可以根据存储在 xml 中的 id 来收集 twitter 数据,但它不会获取所有内容。一段时间后,它只会收到空消息。从 2000 个 ID,我设法保存了 200 条推文。知道如何解决这个问题吗?
import xml.etree.ElementTree as xml
import urllib2
import sys
startIter = int(sys.argv[1])
stopIter = int(sys.argv[2])
#Open file to write JSON to
jsonFile = open('jSONfile', 'a')
#Parse XML directly from the file path
tree = xml.parse("twitter.xml")
#Get the root node
rootElement = tree.getroot()
#Loop through nodes in root
iterator = 1
for node in rootElement:
if iterator >= startIter and iterator <= stopIter:
print iterator
print node[0].text
nodeID = node[0].text
try:
tweet = urllib2.urlopen('https://api.twitter.com/1/statuses/show.json?id={0}&include_entities=true'.format(nodeID))
tweetData = tweet.read()
print tweetData
jsonFile.write('{0}\n'.format(tweetData).',')
except:
pass
iterator = iterator + 1
jsonFile.close()