AttributeError:'NoneType'对象没有属性'lower'-尝试转换为小写时

时间:2019-02-27 22:43:00

标签: python python-3.x

我正在尝试从网页中提取文本,然后将所有文本都转换为小写字母,但是我a,出现object has no attribute错误。可以解决吗?

import requests
from bs4 import BeautifulSoup as bs
import operator

def webpage(url):
    word_list = []
    soup = bs(requests.get(url).text, 'html.parser')
    for text in soup('p', {'class': 'PE7lZb'}):
        content = text.string
        words = content.lower().split()

webpage("https://godan.business.site/")

1 个答案:

答案 0 :(得分:0)

这里的内容与str的类型不同。您应在调用.lower()之前检查内容是否存在

content = text.string
words = content.lower().split() if content and isinstance(content,str) else None