AttributeError:'NoneType'对象在python 3.4中没有属性'lower'

时间:2016-05-24 19:47:30

标签: python beautifulsoup

代码:

package sample

import (
    "sync"
)

var fileMutex = new(sync.RWMutex)

func readFile() {
    fileMutex.RLock()
    defer fileMutex.RUnlock()

    // Read the file. Don't modify it.
}

func writeFile() {
    fileMutex.Lock()
    defer fileMutex.Unlock()

    // Write to the file.
}

错误:

import requests
from bs4 import BeautifulSoup
import operator
def start(url):
    word_list =[]
    source_code =requests.get(url).text
    soup = BeautifulSoup(source_code)
    for post_string in soup.find_all('a',{'class':'cb-skin-ads-link'}):
        content = post_string.string
        words = content.lower()
        for each_word in words:
             print(each_word)
             word_list.append(each_word)

start('http://www.cricbuzz.com/live-cricket-scorecard/16445/gl-vs-rcb-qualifier-1-indian-premier-league-2016')

2 个答案:

答案 0 :(得分:1)

要了解发生了什么,我们应该看一下documentation。有一种情况.stringNone

  

如果一个标签包含多个东西,那么不清楚.string应该引用什么,所以.string被定义为None

您应该考虑使用get_text()来考虑元素的子元素:

content = post_string.get_text()

请注意,这有助于避免错误,但您仍然无法输出,因为您找到的元素实际上没有任何文本:

<a target="_blank" href="Javascript:void(0)" class="cb-skin-ads-link cb-skin-ads-link-fixed ad-skin" rel="noreferrer"></a>

答案 1 :(得分:0)

出于某种原因,内容。尝试使用调试器或打印语句来查看您从 soup.find_all 获得的内容。似乎列表不是空的,但至少一个元素的字符串属性为None。