2017-01-15 96 views
0

以下是錯誤:Python的錯誤 - AttributeError的: 'NoneType' 對象沒有屬性 '分裂'

文件 「F ** PY。」,第34行,在模塊

url_type = url.split('-')[0][-2:] # 

這裏被整個街區:

fit_urls = [] 
for event_url in event_urls: 
    print event_url 
    try: 
    sock = urllib.urlopen(event_url) 
    event_html = sock.read() 
    event_soup = BeautifulSoup(event_html) 

    tds = event_soup.find_all('td') 
    for td in tds: 
     for link in td.find_all('a'): 
      url = link.get('href') 
      url_type = url.split('-')[0][-2:] letters 
      if url_type == 'ht': 
       #print url 
       fit_urls.append(url) 

except HTTPError: 
    pass 

`

+0

沒有鏈接。 'link.get'產生'None',因爲裏面沒有''href''。 – TigerhawkT3

+0

它看起來像'url = link.get('href')'返回'None',即鏈接中沒有找到URL(或者根據庫的行爲方式找不到鏈接) – user783836

回答

0

那是因爲你的任何'link'是不具有'href'屬性。您可以在做url = link.get('href')之前添加print link來驗證它。

爲了解決這個問題,你可以添加額外的if檢查過濾等環節爲:

for td in tds: 
    for link in td.find_all('a'): 
     url = link.get('href') 
     if url: # additional check. will be `False` when `'url'` will be `None` 
      url_type = url.split('-')[0][-2:] letters 
      # Your rest of the code 
0

看起來url = link.get('href')將返回None。你可以在你的循環中檢查None

for td in tds: 
    for link in td.find_all('a'): 
     url = link.get('href') 
     if not url: 
      continue 
     url_type = url.split('-')[0][-2:] letters 
     if url_type == 'ht': 
      #print url 
      fit_urls.append(url) 
相關問題