2017-08-11 42 views
-1
consumer_key = '' 
consumer_secret = '' 
access_token = '' 
access_token_secret = '' 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 
auth.secure = True 
api = tweepy.API(auth) 

oo = api.user_timeline(screen_name = 'OpenOutcrier',count=10) 

wb = openpyxl.load_workbook('morning.xlsx') 
am_sheet = wb.active 

for x in oo: 
    tweettxt = x.text 
    link2remove = 'http\w+' 
    relink = re.compile(link2remove) 
    if relink in tweettxt: 
     tweettxt = tweettxt.replace(relink, '') 
    print x.created_at, tweettxt 
    am_sheet['%s' % ("H" + str(v))].value = tweettxt 
    v += 1 
    time.sleep(.1) 

time.sleep(1) 

wb.save('morning.xlsx') 

,我發現了錯誤:類型錯誤:強迫爲Unicode:需要字符串或緩衝區,_sre.SRE_Pattern發現

File "C:/Python27/Pyjects/am/morning.py", line 239, in <module> 

    if relink in tweettxt: 

TypeError: coercing to Unicode: need string or buffer, _sre.SRE_Pattern found 

有麻煩這個工作..任何幫助,將不勝感激。假設這是我做錯了重新。

回答

1

the documentation,正則表達式的語法是:

prog = re.compile(pattern) 
result = prog.match(string) 

所以,你想:

relink = re.compile(link2remove) 
if relink.match(tweettxt): 

或者,以取代找到模式:

tweettxt = re.sub(link2remove, '', tweettxt) 
+0

那會做到這一點〜謝謝pchaigno – qorka

+0

你想讓我詳細的接受答案嗎? – pchaigno

相關問題