鱼C论坛

 找回密码
 立即注册
查看: 1588|回复: 5

[已解决]如何用python分词

[复制链接]
发表于 2021-6-4 11:41:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
日常提问时间
  1. from bs4 import BeautifulSoup
  2. import requests,jieba

  3. response = requests.get("https://tianqi.so.com/weather/101230301")#网页信息
  4. response.encoding = "utf-8"
  5. bs = BeautifulSoup(response.text,'html.parser')
  6. wea = bs.find_all('div',class_='temp-realtime')#创造wea的类
  7. wea = wea[0].get_text()
  8. res = jieba.cut(wea)
  9. word = ' '.join(res)
  10. wea = [word[:word.index(' ')]+'°C',word[word.index(' ') + 1:]]
  11. all_wea = bs.find_all('ul',class_='weather-columns')
  12. for i in range(len(all_wea)):
  13.     all_wea[i] = all_wea[i].get_text().replace('\n','')#替换

  14. for j in all_wea:
  15.     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℃,优,持续无风向,微风)
最佳答案
2021-6-20 20:51:08
代码敬上,请给最佳!!!
  1. from bs4 import BeautifulSoup
  2. import requests,jieba

  3. response = requests.get("https://tianqi.so.com/weather/101230301")#网页信息
  4. response.encoding = "utf-8"
  5. bs = BeautifulSoup(response.text,'html.parser')
  6. wea = bs.find_all('div',class_='temp-realtime')#创造wea的类
  7. wea = wea[0].get_text()
  8. res = jieba.cut(wea)
  9. word = ' '.join(res)
  10. wea = [word[:word.index(' ')]+'°C',word[word.index(' ') + 1:]]
  11. all_wea = bs.find_all('ul',class_='weather-columns')
  12. for i in range(len(all_wea)):
  13.     all_wea[i] = all_wea[i].get_text().replace('\n','')#替换

  14. ##all_wea = ["今天 (06-04)                    中雨转小雨  21/24℃优持续无风向 微风",
  15. ##"明天 (06-05)                    小雨转多云  21/25℃优持续无风向 微风",
  16. ##"周日 (06-06)                    晴  19/31℃优持续无风向 微风",
  17. ##"周一 (06-07)                    晴转多云  23/30℃优持续无风向 微风",
  18. ##"周二 (06-08)                    中雨转多云  25/29℃优持续无风向 微风",
  19. ##"周三 (06-09)                    中雨转小雨  25/30℃优持续无风向 微风",
  20. ##"周四 (06-10)                    中雨转多云  25/30℃优持续无风向 微风",
  21. ##"周五 (06-11)                    小雨  22/30℃优西南风 微风",
  22. ##"周六 (06-12)                    小雨转阴  22/29℃优西南风 微风",
  23. ##"周日 (06-13)                    小雨  23/29℃优西风 微风",
  24. ##"周一 (06-14)                    小雨  23/26℃优东南风 微风",
  25. ##"周二 (06-15)                    小雨  23/31℃优东北风 微风",
  26. ##"周三 (06-16)                    小雨  24/29℃优北风 微风",
  27. ##"周四 (06-17)                    小雨转阴  21/25℃优东风 微风",
  28. ##"周五 (06-18)                    阴转小雨  21/28℃优东风 微风"]


  29. for j in all_wea:
  30.     temp = j.split(" ")

  31.     # 去掉多余的空格
  32.     str1 = ""
  33.     for each_str in temp:
  34.         if each_str:
  35.             str1 += each_str + " "
  36.     str1 = str1[:-1]



  37.     temp = str1.split(" ")
  38.    
  39.     date = temp[0]+temp[1]
  40.     wather = temp[2]
  41.     infos = temp[3]
  42.     wind_speed = temp[4]
  43.     temperature = infos.split("℃")[0]+"℃"
  44.     rate = infos[len(temperature)]
  45.     wind_diraction = infos[len(temperature)+1:]

  46.     # 完美收工,打印结果
  47.     print(date,wather,wind_speed,temperature,rate,wind_diraction)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-19 09:51:47 | 显示全部楼层
jieba分词
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-20 15:42:19 | 显示全部楼层
刚刚找到了更简便的方法
利用.split()语句
  1. from bs4 import BeautifulSoup
  2. import requests,jieba

  3. response = requests.get("https://tianqi.so.com/weather/101230301")#网页信息
  4. response.encoding = "utf-8"
  5. bs = BeautifulSoup(response.text,'html.parser')
  6. wea = bs.find_all('div',class_='temp-realtime')#创造wea的类
  7. wea = wea[0].get_text()
  8. res = jieba.cut(wea)
  9. word = ' '.join(res)
  10. wea = [word[:word.index(' ')]+'°C',word[word.index(' ') + 1:]]
  11. all_wea = bs.find_all('ul',class_='weather-columns')
  12. for index in range(len(all_wea)):
  13.     all_wea[index] = all_wea[index].get_text().replace('\n','')

  14. for index in range(15):
  15.     content = all_wea[index].split(' ')
  16.     all_wea.append(content)

  17. del all_wea[:15]
  18. for j in range(15):
  19.     for i in range(20):
  20.             all_wea[j].remove('')
复制代码

请问还能再化简吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-20 20:31:58 | 显示全部楼层
正则表达式...第一个就把)括号前的全取下来,第二个就用空格来取,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-20 20:51:08 | 显示全部楼层    本楼为最佳答案   
代码敬上,请给最佳!!!
  1. from bs4 import BeautifulSoup
  2. import requests,jieba

  3. response = requests.get("https://tianqi.so.com/weather/101230301")#网页信息
  4. response.encoding = "utf-8"
  5. bs = BeautifulSoup(response.text,'html.parser')
  6. wea = bs.find_all('div',class_='temp-realtime')#创造wea的类
  7. wea = wea[0].get_text()
  8. res = jieba.cut(wea)
  9. word = ' '.join(res)
  10. wea = [word[:word.index(' ')]+'°C',word[word.index(' ') + 1:]]
  11. all_wea = bs.find_all('ul',class_='weather-columns')
  12. for i in range(len(all_wea)):
  13.     all_wea[i] = all_wea[i].get_text().replace('\n','')#替换

  14. ##all_wea = ["今天 (06-04)                    中雨转小雨  21/24℃优持续无风向 微风",
  15. ##"明天 (06-05)                    小雨转多云  21/25℃优持续无风向 微风",
  16. ##"周日 (06-06)                    晴  19/31℃优持续无风向 微风",
  17. ##"周一 (06-07)                    晴转多云  23/30℃优持续无风向 微风",
  18. ##"周二 (06-08)                    中雨转多云  25/29℃优持续无风向 微风",
  19. ##"周三 (06-09)                    中雨转小雨  25/30℃优持续无风向 微风",
  20. ##"周四 (06-10)                    中雨转多云  25/30℃优持续无风向 微风",
  21. ##"周五 (06-11)                    小雨  22/30℃优西南风 微风",
  22. ##"周六 (06-12)                    小雨转阴  22/29℃优西南风 微风",
  23. ##"周日 (06-13)                    小雨  23/29℃优西风 微风",
  24. ##"周一 (06-14)                    小雨  23/26℃优东南风 微风",
  25. ##"周二 (06-15)                    小雨  23/31℃优东北风 微风",
  26. ##"周三 (06-16)                    小雨  24/29℃优北风 微风",
  27. ##"周四 (06-17)                    小雨转阴  21/25℃优东风 微风",
  28. ##"周五 (06-18)                    阴转小雨  21/28℃优东风 微风"]


  29. for j in all_wea:
  30.     temp = j.split(" ")

  31.     # 去掉多余的空格
  32.     str1 = ""
  33.     for each_str in temp:
  34.         if each_str:
  35.             str1 += each_str + " "
  36.     str1 = str1[:-1]



  37.     temp = str1.split(" ")
  38.    
  39.     date = temp[0]+temp[1]
  40.     wather = temp[2]
  41.     infos = temp[3]
  42.     wind_speed = temp[4]
  43.     temperature = infos.split("℃")[0]+"℃"
  44.     rate = infos[len(temperature)]
  45.     wind_diraction = infos[len(temperature)+1:]

  46.     # 完美收工,打印结果
  47.     print(date,wather,wind_speed,temperature,rate,wind_diraction)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-7-26 16:56:06 | 显示全部楼层
nahongyan1997 发表于 2021-6-20 20:51
代码敬上,请给最佳!!!

还算一种新鲜的方法吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 10:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表