python爬取天气预报信息

代码如下:

from bs4 import BeautifulSoup
import requests
url="http://www.weather.com.cn/weather/101010200.shtml"

resp = requests.get(url)
#print(resp.text)

#print(resp.content.decode('utf8'))

html = resp.content.decode('utf8')

soup = BeautifulSoup(html,'html.parser')
body = soup.body
data = body.find("div",{"id":"7d"})
ul = data.find("ul")
li = ul.find_all("li")
print(li)

for day in li:
        temp = []
        date=day.find_all("h1")
        temp.append(date)
        inf=day.find_all("p")
        temp.append(inf[0].string) 
        print(inf[0])

执行结果如下
python爬取天气预报信息_第1张图片
python爬取天气预报信息_第2张图片
喜欢就点个关注呗!!

你可能感兴趣的:(python)