[英]AttributeError: 'NoneType' object has no attribute 'split'?
[英]AttributeError: 'NoneType' object has no attribute 'split'
我有一個包含這兩個函數的腳本:
# Getting content of each page
def GetContent(url):
response = requests.get(url)
return response.content
# Extracting the sites
def CiteParser(content):
soup = BeautifulSoup(content)
print "---> site #: ",len(soup('cite'))
result = []
for cite in soup.find_all('cite'):
result.append(cite.string.split('/')[0])
return result
當我運行程序時,我有以下錯誤:
result.append(cite.string.split('/')[0])
AttributeError: 'NoneType' object has no attribute 'split'
輸出樣本:
URL: <URL That I use to search 'can be google, bing, etc'>
---> site #: 10
site1.com
.
.
.
site10.com
URL: <URL That I use to search 'can be google, bing, etc'>
File "python.py", line 49, in CiteParser
result.append(cite.string.split('/')[0])
AttributeError: 'NoneType' object has no attribute 'split'
可能會發生,字符串里面沒有任何東西,而不是“無”類型,所以我可以假設首先檢查你的字符串是不是“無”
# Extracting the sites
def CiteParser(content):
soup = BeautifulSoup(content)
#print soup
print "---> site #: ",len(soup('cite'))
result = []
for cite in soup.find_all('cite'):
if cite.string is not None:
result.append(cite.string.split('/'))
print cite
return result
for cite in soup.find_all('cite'):
if( (cite.string is None) or (len(cite.string) == 0)):
continue
result.append(cite.string.split('/')[0])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.