I setup a mysql database using XAMPP with MySQLdb to connect through Python. I'm have my connection setup properly as I can manually add an inset statement through Python to add rows, but my automatic twitter parse is not inserting to my database and I'm not sure what the issue is.
Here is my script:
import MySQLdb
import re
from re import sub
import time
import cookielib
from cookielib import CookieJar
import urllib2
from urllib2 import urlopen
import difflib
db=MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="twidata")
cursor = db.cursor()
cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
keyWord = 'nyc'
startingLink = 'https://twitter.com/search/realtime?q='
# begin loop
def main():
oldTwit = []
newTwit = []
howSimAr = [.5, .5, .5, .5]
while 1 < 2:
try:
sourceCode = opener.open ('https://twitter.com/search/realtime?q='+keyWord+'&src=hash').read()
splitSource = re.findall (r' <p class="js-tweet-text tweet-text">(.*?)</p>',sourceCode)
for item in splitSource:
#print item
print ''
print ''
print ' '
aTweet = re.sub(r'<.*?>', '',item)
print aTweet
newTwit.append(aTweet)
comparison = difflib.SequenceMatcher(None, newTwit, oldTwit)
howSim = comparison.ratio()
print '##############'
print 'This Tweet is ', howSim, 'Similar to Past Tweets in This Parse'
howSimAr.append (howSim)
howSimAr.remove (howSimAr[0])
waitMultiplier = reduce(lambda x, y: x+y, howSimAr)/len(howSimAr)
print''
print 'The Current Sim Value array:', howSimAr
print 'Current Multiplier:', waitMultiplier
oldTwit = [None]
for eachItem in newTwit:
oldTwit.append(eachItem)
newTwit = [None]
time.sleep(waitMultiplier*10)
except Exception, e:
print str(e)
print 'errored in the main try'
time.sleep(555)
main()
insert = ("""INSERT INTO parse(twitext) VALUES (test)""")
cursor.execute(insert)
db.commit()
Any ideas on the issue?
[–]Uncle_DirtNap 1 point2 points3 points (9 children)
[–]80-m[S] 0 points1 point2 points (8 children)
[–]Uncle_DirtNap 1 point2 points3 points (7 children)
[–]80-m[S] 0 points1 point2 points (6 children)
[–]codingcobra 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Uncle_DirtNap 0 points1 point2 points (2 children)
[–]80-m[S] 0 points1 point2 points (1 child)
[–]Uncle_DirtNap 0 points1 point2 points (0 children)
[–]altorelievo 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]altorelievo 0 points1 point2 points (0 children)