附件
python爬天气预报
from urllib.request import urlopen
#获取页面
from bs4 import BeautifulSoup
#从网页抓取数据
html=urlopen(r"http://www.weather.com.cn/weather/101090101.shtml").read().decode('utf-8')
对中文解码
#print(html)
soup=BeautifulSoup(html,features="lxml")
all_ul=soup.find_all("ul",attrs={"class":"t clearfix"})
all_li=all_ul[0].find_all('li')
for i in all_li:
h1=i.find("h1").get_text()
p1=i.find("p",attrs={"class":"wea"}).get_text()
p2=i.find("p",attrs={"class":"tem"}).get_text()
print("==============")
print(h1)
print(p1)
print(p2)