鱼C论坛

 找回密码
 立即注册
查看: 1371|回复: 2

格式化换行输出问题

[复制链接]
发表于 2018-4-17 16:08:39 | 显示全部楼层 |阅读模式

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

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

x
正在写一个爬虫程序爬墨迹天气
现在遇到一个把抓取到的信息按格式输出的问题,不知道如何换行格式化输出
代码如下:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. import sys
  4. sys.path.append('C:\Python27\Lib\site-packages')
  5. import re
  6. from wxpy import *

  7. url = 'https://tianqi.moji.com/weather/china/guangdong/tianhe-district'

  8. headers = {
  9.     'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
  10.     'Content-Type': 'application/x-www-form-urlencoded',
  11.     'Connection': 'Keep-Alive',
  12.     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  13. }

  14. html = requests.get(url, headers=headers).text


  15. soup = BeautifulSoup(html, "lxml")

  16. weather_all = {}

  17. # 当前天气\温度\更新时间\湿度\风向风力\温馨提示\空气质量
  18. weather_all['current_weather'] = re.search(r'>([\u4e00-\u9fa5]{1,2})</', str(soup.select('.wea_weather b'))).group(1)
  19. weather_all['current_temperature'] = re.search(r'>(\d\d?)</', str(soup.select('.wea_weather em'))).group(1)
  20. weather_all['update_time'] = re.search(r'\u5929(\d\d?:\d\d?)\u66f4', str(soup.select('.wea_weather strong'))).group(1)
  21. weather_all['humidity'] = re.search(r'n>(\u6e7f\u5ea6\s\d\d?%)</s', str(soup.select('.wea_about span'))).group(1)
  22. weather_all['wind'] = re.search(r'm>([\u4e00-\u9fa5]+?\d+?\u7ea7)</', str(soup.select('.wea_about em'))).group(1)
  23. weather_all['tips'] = re.search(r'm>([\u4e00-\u9fa5]+?[,]?[\u4e00-\u9fa5]+?)。?</', str(soup.select('.wea_tips em'))).group(1)
  24. weather_all['aircon'] = re.search(r'em>(\d\d?\s[\u4e00-\u9fa5]+?)</e', str(soup.select('.wea_alert em'))).group(1)

  25. # 提取天气预报内容
  26. weathercast_list = soup.select('.forecast li')
  27. # 今天天气\温度\风向\风力\空气质量
  28. weather_all['today_weather'] = re.search(r'"(.+?)"', str(weathercast_list[4])).group(1)
  29. weather_all['today_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[5])).group(1)
  30. weather_all['today_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[6])).group(1)
  31. weather_all['today_wind_force'] = re.search(r'<b>(..)</b>', str(weathercast_list[6])).group(1)
  32. weather_all['today_aircon'] = re.search(r'">\s+(\d\d\s[\u4e00-\u9fa5]?)\s+?</st', str(weathercast_list[7])).group(1)

  33. # 明天天气\温度\风向\风力\空气质量
  34. weather_all['tmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list[9])).group(1)
  35. weather_all['tmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[10])).group(1)
  36. weather_all['tmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[11])).group(1)
  37. weather_all['tmr_wind_force'] = re.search(r'<b>(..)</b>', str(weathercast_list[11])).group(1)
  38. weather_all['tmr_aircon'] = re.search(r'">\s+(\d\d\s[\u4e00-\u9fa5]?)\s+?</st', str(weathercast_list[12])).group(1)

  39. # 后天天气\温度\风向\风力\空气质量
  40. weather_all['datmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list[14])).group(1)
  41. weather_all['datmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[15])).group(1)
  42. weather_all['datmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[16])).group(1)
  43. weather_all['datmr_wind_force'] = re.search(r'<b>(..)</b>', str(weathercast_list[16])).group(1)
  44. weather_all['datmr_aircon'] = re.search(r'">\s+(\d\d\s[\u4e00-\u9fa5]?)\s+?</st', str(weathercast_list[17])).group(1)


  45. msg = "******Python机器人为您播报天气预报******\n"
  46.       "当前天气信息:更新于%s\n"
  47.       "天气:%s\n"
  48.       "气温:%s\n".format(weather_all['update_time'], weather_all['current_weather'], weather_all['current_temperature'])

  49. print(msg)
复制代码


希望msg可以按以下格式输出, 但是照我上面这么用格式化只能实现换行,但是格式化没办法实现,请问怎么写才行呢?

------------天气信息------------
当前天气信息(更新于:14点25分):

天气:
气温:
湿度:
风向\风力:
空气质量:
温馨提示:


------------三日天气预报------------

***今天***
天气:
气温:
风向:
风力:
空气质量:

***明天***
天气:
气温:
风向:
风力:
空气质量:

***后天***
天气:
气温:
风向:
风力:
空气质量:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-4-17 16:22:36 | 显示全部楼层
我又自己解决了。。。。

  1. msg = []
  2. msg.append('***天气预报***')
  3. msg.append('当前天气信息(更新于%s)'% (weather_all['update_time']))

  4. msgcontent = '\n'.join(msg)

  5. print(msgcontent)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-30 00:10:56 | 显示全部楼层
heywilliam 发表于 2018-4-17 16:22
我又自己解决了。。。。

楼主,我请教一下'%S\n'这两个配合使用是格式化什么内容,实现换行吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-29 12:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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