新手求教爬虫问题
from bs4 import BeautifulSoupimport requests
response=requests.get('http://www.weather.com.cn/weather/101100101.shtml')
response.encoding='utf-8'
soup=BeautifulSoup(response.text,'html.parser')
div=soup.find('div',id='7d')
ul=div.find('ul')
li=ul.find_all('li')
weatherForSevenDay=[]
for weather in li:
temp=[]
date=weather.find('h1').string
temp.append(date)
p=weather.find_all('p')
weatherDetail=p.string
if weatherDetail is not None:
temp.append('天气为:%s'%weatherDetail)
Hightemprature=p.find('span').string #这里去掉.string就没问,加上就会报错,下面的都没问题
if Hightemprature is not None:
temp.append('最高温为:%s'%Hightemprature)
LowTemprature=p.find('i').string
if LowTemprature is not None:
temp.append('最低温为:%s'%LowTemprature)
weatherForSevenDay.append(temp)
for weather in weatherForSevenDay:
print(weather)
加上就会报错
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\Python练习程序\untitled3.py", line 25, in <module>
Hightemprature=p.find('span').string
AttributeError: 'NoneType' object has no attribute 'string'
去掉的结果是这样的:
['10日(今天)', '天气为:多云', '最低温为:12℃']
['11日(明天)', '天气为:晴转多云', '最高温为:<span>26℃</span>', '最低温为:13℃']
['12日(后天)', '天气为:多云', '最高温为:<span>27℃</span>', '最低温为:14℃']
['13日(周日)', '天气为:多云', '最高温为:<span>26℃</span>', '最低温为:13℃']
['14日(周一)', '天气为:小雨转多云', '最高温为:<span>25℃</span>', '最低温为:10℃']
['15日(周二)', '天气为:阴转晴', '最高温为:<span>23℃</span>', '最低温为:10℃']
['16日(周三)', '天气为:多云', '最高温为:<span>26℃</span>', '最低温为:9℃']
页:
[1]