|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
正在写一个爬虫程序爬墨迹天气
现在遇到一个把抓取到的信息按格式输出的问题,不知道如何换行格式化输出
代码如下:
- import requests
- from bs4 import BeautifulSoup
- import sys
- sys.path.append('C:\Python27\Lib\site-packages')
- import re
- from wxpy import *
- url = 'https://tianqi.moji.com/weather/china/guangdong/tianhe-district'
- headers = {
- 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Connection': 'Keep-Alive',
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
- }
- html = requests.get(url, headers=headers).text
- soup = BeautifulSoup(html, "lxml")
- weather_all = {}
- # 当前天气\温度\更新时间\湿度\风向风力\温馨提示\空气质量
- weather_all['current_weather'] = re.search(r'>([\u4e00-\u9fa5]{1,2})</', str(soup.select('.wea_weather b'))).group(1)
- weather_all['current_temperature'] = re.search(r'>(\d\d?)</', str(soup.select('.wea_weather em'))).group(1)
- weather_all['update_time'] = re.search(r'\u5929(\d\d?:\d\d?)\u66f4', str(soup.select('.wea_weather strong'))).group(1)
- weather_all['humidity'] = re.search(r'n>(\u6e7f\u5ea6\s\d\d?%)</s', str(soup.select('.wea_about span'))).group(1)
- weather_all['wind'] = re.search(r'm>([\u4e00-\u9fa5]+?\d+?\u7ea7)</', str(soup.select('.wea_about em'))).group(1)
- weather_all['tips'] = re.search(r'm>([\u4e00-\u9fa5]+?[,]?[\u4e00-\u9fa5]+?)。?</', str(soup.select('.wea_tips em'))).group(1)
- weather_all['aircon'] = re.search(r'em>(\d\d?\s[\u4e00-\u9fa5]+?)</e', str(soup.select('.wea_alert em'))).group(1)
- # 提取天气预报内容
- weathercast_list = soup.select('.forecast li')
- # 今天天气\温度\风向\风力\空气质量
- weather_all['today_weather'] = re.search(r'"(.+?)"', str(weathercast_list[4])).group(1)
- weather_all['today_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[5])).group(1)
- weather_all['today_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[6])).group(1)
- weather_all['today_wind_force'] = re.search(r'<b>(..)</b>', str(weathercast_list[6])).group(1)
- weather_all['today_aircon'] = re.search(r'">\s+(\d\d\s[\u4e00-\u9fa5]?)\s+?</st', str(weathercast_list[7])).group(1)
- # 明天天气\温度\风向\风力\空气质量
- weather_all['tmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list[9])).group(1)
- weather_all['tmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[10])).group(1)
- weather_all['tmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[11])).group(1)
- weather_all['tmr_wind_force'] = re.search(r'<b>(..)</b>', str(weathercast_list[11])).group(1)
- weather_all['tmr_aircon'] = re.search(r'">\s+(\d\d\s[\u4e00-\u9fa5]?)\s+?</st', str(weathercast_list[12])).group(1)
- # 后天天气\温度\风向\风力\空气质量
- weather_all['datmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list[14])).group(1)
- weather_all['datmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[15])).group(1)
- weather_all['datmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[16])).group(1)
- weather_all['datmr_wind_force'] = re.search(r'<b>(..)</b>', str(weathercast_list[16])).group(1)
- weather_all['datmr_aircon'] = re.search(r'">\s+(\d\d\s[\u4e00-\u9fa5]?)\s+?</st', str(weathercast_list[17])).group(1)
- msg = "******Python机器人为您播报天气预报******\n"
- "当前天气信息:更新于%s\n"
- "天气:%s\n"
- "气温:%s\n".format(weather_all['update_time'], weather_all['current_weather'], weather_all['current_temperature'])
- print(msg)
复制代码
希望msg可以按以下格式输出, 但是照我上面这么用格式化只能实现换行,但是格式化没办法实现,请问怎么写才行呢?
------------天气信息------------
当前天气信息(更新于:14点25分):
天气:
气温:
湿度:
风向\风力:
空气质量:
温馨提示:
------------三日天气预报------------
***今天***
天气:
气温:
风向:
风力:
空气质量:
***明天***
天气:
气温:
风向:
风力:
空气质量:
***后天***
天气:
气温:
风向:
风力:
空气质量:
|
|