2017-02-27 139 views
0

我已经做了这部分代码,但它表明我这个错误,当我跑这NoneType'对象在python中没有属性?

for tmp in links: 
    jobref=re.search('jobId=(\d+)&', tmp).group()+".html" 
    print(jobref) 
    if tmp not in os.listdir('.'): 
     file=open(jobref,"w+") 
     file.write(urllib.urlopen(tmp).read()) 

AttributeError的:“NoneType”对象有没有属性“组”

就如何解决它的主意?

+4

're.search'可能返回'None' –

回答

0

您在字符串tmp中没有所需的子字符串,因此re.search返回NoneNone有ho属性group()。您必须在group()方法调用之前检查返回类型re.search

result = re.search('jobId=(\d+)&', tmp) 
if result: 
    jobref = result.group() + ".html" 
else: 
    ... 
相关问题