|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
日常提问时间
- from bs4 import BeautifulSoup
- import requests,jieba
- response = requests.get("https://tianqi.so.com/weather/101230301")#网页信息
- response.encoding = "utf-8"
- bs = BeautifulSoup(response.text,'html.parser')
- wea = bs.find_all('div',class_='temp-realtime')#创造wea的类
- wea = wea[0].get_text()
- res = jieba.cut(wea)
- word = ' '.join(res)
- wea = [word[:word.index(' ')]+'°C',word[word.index(' ') + 1:]]
- all_wea = bs.find_all('ul',class_='weather-columns')
- for i in range(len(all_wea)):
- all_wea[i] = all_wea[i].get_text().replace('\n','')#替换
- for j in all_wea:
- print(j)#输出
复制代码
输出:
今天 (06-04) 中雨转小雨 21/24℃优持续无风向 微风
明天 (06-05) 小雨转多云 21/25℃优持续无风向 微风
周日 (06-06) 晴 19/31℃优持续无风向 微风
周一 (06-07) 晴转多云 23/30℃优持续无风向 微风
周二 (06-08) 中雨转多云 25/29℃优持续无风向 微风
周三 (06-09) 中雨转小雨 25/30℃优持续无风向 微风
周四 (06-10) 中雨转多云 25/30℃优持续无风向 微风
周五 (06-11) 小雨 22/30℃优西南风 微风
周六 (06-12) 小雨转阴 22/29℃优西南风 微风
周日 (06-13) 小雨 23/29℃优西风 微风
周一 (06-14) 小雨 23/26℃优东南风 微风
周二 (06-15) 小雨 23/31℃优东北风 微风
周三 (06-16) 小雨 24/29℃优北风 微风
周四 (06-17) 小雨转阴 21/25℃优东风 微风
周五 (06-18) 阴转小雨 21/28℃优东风 微风
请问,如何把这些单独的词分离出来(如:明天 (06-05),小雨转多云,21/25℃,优,持续无风向,微风)
代码敬上,请给最佳!!!
- from bs4 import BeautifulSoup
- import requests,jieba
- response = requests.get("https://tianqi.so.com/weather/101230301")#网页信息
- response.encoding = "utf-8"
- bs = BeautifulSoup(response.text,'html.parser')
- wea = bs.find_all('div',class_='temp-realtime')#创造wea的类
- wea = wea[0].get_text()
- res = jieba.cut(wea)
- word = ' '.join(res)
- wea = [word[:word.index(' ')]+'°C',word[word.index(' ') + 1:]]
- all_wea = bs.find_all('ul',class_='weather-columns')
- for i in range(len(all_wea)):
- all_wea[i] = all_wea[i].get_text().replace('\n','')#替换
- ##all_wea = ["今天 (06-04) 中雨转小雨 21/24℃优持续无风向 微风",
- ##"明天 (06-05) 小雨转多云 21/25℃优持续无风向 微风",
- ##"周日 (06-06) 晴 19/31℃优持续无风向 微风",
- ##"周一 (06-07) 晴转多云 23/30℃优持续无风向 微风",
- ##"周二 (06-08) 中雨转多云 25/29℃优持续无风向 微风",
- ##"周三 (06-09) 中雨转小雨 25/30℃优持续无风向 微风",
- ##"周四 (06-10) 中雨转多云 25/30℃优持续无风向 微风",
- ##"周五 (06-11) 小雨 22/30℃优西南风 微风",
- ##"周六 (06-12) 小雨转阴 22/29℃优西南风 微风",
- ##"周日 (06-13) 小雨 23/29℃优西风 微风",
- ##"周一 (06-14) 小雨 23/26℃优东南风 微风",
- ##"周二 (06-15) 小雨 23/31℃优东北风 微风",
- ##"周三 (06-16) 小雨 24/29℃优北风 微风",
- ##"周四 (06-17) 小雨转阴 21/25℃优东风 微风",
- ##"周五 (06-18) 阴转小雨 21/28℃优东风 微风"]
- for j in all_wea:
- temp = j.split(" ")
- # 去掉多余的空格
- str1 = ""
- for each_str in temp:
- if each_str:
- str1 += each_str + " "
- str1 = str1[:-1]
- temp = str1.split(" ")
-
- date = temp[0]+temp[1]
- wather = temp[2]
- infos = temp[3]
- wind_speed = temp[4]
- temperature = infos.split("℃")[0]+"℃"
- rate = infos[len(temperature)]
- wind_diraction = infos[len(temperature)+1:]
- # 完美收工,打印结果
- print(date,wather,wind_speed,temperature,rate,wind_diraction)
复制代码
|
|