程序参考:SPOTO世博在线教育
import requests
from bs4 import BeautifulSoup
import xlwt
import time
headers = {
'User-Agent':'Mozilla/5.0'}
url = "http://www.weather.com.cn/weather/101270101.shtml"
weather_str = requests.get(url,headers = headers)
weather_str_content = weather_str.content
#print(weather_str_content)
soup = BeautifulSoup(weather_str_content,'html.parser')
soup_ul = soup.find('ul',"t clearfix")
#print(soup_ul)
#提取数据
soup_li_list = soup_ul.find_all("li")
weather_list = []
for i in range(len(soup_li_list)):
#天气
weather_data = soup_li_list[i].find("h1").get_text()
#print(weather_data)
#天气
weather_wea = soup_li_list