chsiwe 2022-05-03 14:58 采纳率: 50%
浏览 1605
已结题

'NoneType' object has no attribute 'string'错误

问题相关代码
from bs4 import BeautifulSoup
import requests
response = requests.get('http://www.weather.com.cn/weather/101280101.shtml')#发起网络请求(访问网址)
response.encoding = 'utf-8'#设置编码为utf-8
soup = BeautifulSoup(response.text,'html.parser')
div = soup.find('div',id = '7d')#获取id=7d的div标签的内容
ul = div.find('ul')#获取div标签中第一个ul标签的内容
li = ul.find_all('li')#获取ul标签中所有li标签的内容
weatherForSevenDay = []
for weather in li:
    temp = []
    date = weather.find('h1').string#获取li标签中h1标签的内容
    temp.append(date)
    p = weather.find_all('p')#获取li标签中所有p标签的内容
    weatherDetail = p[0].string#获取第一个p标签的内容
    if weatherDetail is not None:
        temp.append('天气为:%s'%weatherDetail)
    HighTemprature = p[1].find('span').string#获取第二个p标签中span标签的内容
    if HighTemprature is not None:
        temp.append('最高温为%s°C'%HighTemprature)
    LowTemprature = p[1].find('i').string#获取第二个p标签中i标签的内容
    if LowTemprature is not None:
        temp.append('最低温为%s°C'%LowTemprature)
    weatherForSevenDay.append(temp)
for weather in weatherForSevenDay:
    print(weather)

######报错

Traceback (most recent call last):
File "C:\Users\DELL\Documents\Aptana Studio 3 Workspace\python\my\广州七日天气.py", line 19, in
HighTemprature = p[1].find('span').string#获取第二个p标签中span标签的内容
AttributeError: 'NoneType' object has no attribute 'string'

展开全部

  • 写回答

2条回答 默认 最新

  • 溪风沐雪 2022-05-03 15:09
    关注

    看错误提示是因为p[1].find('span')没有找到这个标签,是一个空值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    溪风沐雪 2022-05-03 15:17

    我打印了一下你的weather,内容如下:
    并不能按你的解析方式去解析,有些数据没有

    
    <li class="sky skyid lv1 on">
    <h1>3日(今天)</h1>
    <big class="png40"></big>
    <big class="png40 n00"></big>
    <p class="wea" title="晴"></p>
    <p class="tem">
    <i>15℃</i>
    </p>
    <p class="win">
    <em>
    <span class="NNW" title="无持续风向"></span>
    </em>
    <i>&lt;3级</i>
    </p>
    <div class="slid"></div>
    </li>
    

    回复
    溪风沐雪 2022-05-03 15:22

    我给你改了一下,能正常运行了,你可以参考一下:

    from bs4 import BeautifulSoup
    import requests
    response = requests.get('http://www.weather.com.cn/weather/101280101.shtml')#发起网络请求(访问网址)
    response.encoding = 'utf-8'#设置编码为utf-8
    soup = BeautifulSoup(response.text,'html.parser')
    div = soup.find('div',id = '7d')#获取id=7d的div标签的内容
    ul = div.find('ul')#获取div标签中第一个ul标签的内容
    li = ul.find_all('li')#获取ul标签中所有li标签的内容
    weatherForSevenDay = []
    for weather in li:
        temp = []
        date = weather.find('h1').string#获取li标签中h1标签的内容
        temp.append(date)
        p = weather.find_all('p')#获取li标签中所有p标签的内容
        print(p)
        weatherDetail = p[0].string#获取第一个p标签的内容
        if weatherDetail is not None:
            temp.append('天气为:%s'%weatherDetail)
        HighTemprature = p[1].find('i').string#获取第二个p标签中span标签的内容
        if HighTemprature is not None:
            temp.append('气温为%s°C'%HighTemprature)
        LowTemprature = p[2].find('i').string#获取第二个p标签中i标签的内容
        if LowTemprature is not None:
            temp.append('风力为%s'%LowTemprature)
        weatherForSevenDay.append(temp)
    for weather in weatherForSevenDay:
        print(weather)
    

    img

    3
    回复
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 5月11日
  • 已采纳回答 5月4日
  • 创建了问题 5月3日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部