‘NoneType‘ object has no attribute ‘text‘

python3
pycharm
小白写一个爬三国演义的代码,报这个错
说是content = div_tag.text这里'NoneType' object has no attribute 'text'
谁知道
原因竟然是detail_url = 'https://www.shicimingju.com/'+ li.a['href']多了一个/
这八竿子打不着啊。。。。

删掉后 detail_url = 'https://www.shicimingju.com'+ li.a['href'] 成功

结果:
‘NoneType‘ object has no attribute ‘text‘_第1张图片

from bs4 import BeautifulSoup
import requests

if __name__ == "__main__":
    headers = {
                             'User-Agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/83.0.4103.116Safari/537.36'
                }
    url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
    res = requests.get(url=url,headers=headers)
    page_text = res.text
    # 实例化对象
    soup = BeautifulSoup(page_text,'lxml')
    # 解析
    li_list = soup.select('.book-mulu > ul > li')
    fp = open('./sanguo.text','w',encoding='utf-8')
    for li in li_list:
        title = li.a.string
        detail_url = 'https://www.shicimingju.com'+ li.a['href']
        # 对详情页发起请求
        detail_page_text = requests.get(url=detail_url,headers=headers).text
        # 解析出详情页内容
        detail_soup = BeautifulSoup(detail_page_text,'lxml')
        div_tag = detail_soup.find('div',class_='chapter_content')
        # 拿到内容
        content = div_tag.text
        fp.write(title+':'+content+'\n')
        print(title,'爬取成功!!')

你可能感兴趣的:(python报错,python)