极其简单的爬虫,但是第五行老是报错

import re
doc_t='D:/读后感/唐朝.doc'
f=open(doc_t,'r')
f.close()
h=re.findall('唐朝(.?)贪官',f,re.S)
print(h)
第五行报错内容:
Exception has occurred: error
multiple repeat at position 5
File "C:\Users\EMILY\Desktop\python\SCRAPY try\try2", line 5, in h=re.findall('唐朝(.?
)贪官',f,re.S)
请问该怎么解决?

你的文件在读取前已经关闭了
f.close()
应该在
h=re.findall('唐朝(.?)贪官',f,re.S)
之后f.close()


import re
doc_t='D:/读后感/唐朝.doc'
f=open(doc_t,'r')
h=re.findall('唐朝(.?)贪官',f,re.S)
print(h)
f.close()

您的采纳是我最大的动力,谢谢!!!

img

这里正则写得有些问题
应该是h=re.findall('唐朝(.?)贪官',f,re.S) #加上
或者
h=re.findall('唐朝(.)贪官',f,re.S) # 将?变

你代码的顺序不对

这篇文章:爬虫 第五讲 多线程爬虫 也许能够解决你的问题,你可以看下