忘了的时候记得翻出来看看
def get_data_7(url):
html = res_text(url)
list_s = html.xpath('//*[@id="7d"]/ul/li') # 获取天气数据列表
Date, Weather, Low, High = [], [], [], []
for i in range(len(list_s)):
#----------------------------根据i还有[0]的遍历用法
list_date = list_s[i].xpath('./h1/text()')[0] # 获取日期,如:4日(明天)
# print(list_data)
list_weather = list_s[i].xpath('./p[1]/@title')[0] # 获取天气情况,如:小雨转雨夹雪
# print(list_weather)
tem_low = list_s[i].xpath('./p[2]/i/text()') # 获取最低气温
tem_high = list_s[i].xpath('./p[2]/span/text()') # 获取最高气温
if tem_high == []: # 遇到夜晚情况,筛掉当天的最高气温
tem_high = tem_low # 无最高气温时,使最高气温等于最低气温
tem_low = int(tem_low[0].replace('℃', '')) # 将气温数据处理
tem_high = int(tem_high[0].replace('℃', ''))
# print(type(tem_high))
Date.append(list_date), Weather.append(list_weather), Low.append(tem_low), High.append(tem_high)
excel = pd.DataFrame() # 定义一个二维列表
excel['日期'] = Date
excel['天气'] = Weather
excel['最低气温'] = Low
excel['最高气温'] = High
# print(excel)
return excel