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')
我收到错误:
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
无法使其正常工作.. 任何帮助将不胜感激。假设这是我在重新做错的事情。
根据文档,正则表达式的语法是:
prog = re.compile(pattern)
result = prog.match(string)
所以你要:
relink = re.compile(link2remove)
if relink.match(tweettxt):
或者,替换找到的模式:
tweettxt = re.sub(link2remove, '', tweettxt)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句