本文分为两个模块,一是爬虫,而是链接微信,发送给好友
首先,我们来分析爬虫模块,爬取得链接地址是
https://tianqi.moji.com/weather/china/,
当我们进入这个页面是,可以选择省份城市,例如我们选择海南三亚,它的地址组成就是
https://tianqi.moji.com/weather/china/hainan/sanya,选择北京的时候,它的地址组成是https://tianqi.moji.com/weather/china/beijing/beijing。这样我们可以基本确定我们在url的组成是由https://tianqi.moji.com/weather/china/+省份拼音+城市拼音
或https://tianqi.moji.com/weather/china/+直辖市拼音+城市拼音。所以,我们需要引入第三方库,xpinyin ,在此,有两个省份,陕西,山西由于两个省份拼音一样,陕西省得pinyin就有一些改变。下面我们直接选择城市上代码
def choice():
print('请输入 1 代表直辖市 2 代表非直辖市')
num=input()
if num=='1':
city=input("请输入城市:")
city_pin=pin.get_pinyin(city,'')
get_info(None,city_pin,None,city)
else:
prov=input("请输入省份:")
city=input("请输入城市:")
if prov=='陕西':
prov_pin='shaanxi'
city_pin=pin.get_pinyin(city,'')
get_info(prov_pin,city_pin,prov,city)
else:
prov_pin=pin.get_pinyin(prov,'') #将汉字转# 为拼音
city_pin=pin.get_pinyin(city,'')
get_info(prov_pin,city_pin,prov,city)
当我们准确获取了爬取城市的链接后 需要对页面进行分析,提取我们需要的信息,在此提醒,例如北京天津才有限号,下面是页面分析的具体代码,
def get_info(prov_pin,city_pin,prov,city):
if prov_pin==None:
url='https://tianqi.moji.com/weather/china/'
url=url+city_pin+'/'+city_pin
else:
url='https://tianqi.moji.com/weather/china/'
url=url+prov_pin+'/'+city_pin
print(url)
#获取天气信息begin#
htmlData=request.urlopen(url).read().decode('utf8')
data=BeautifulSoup(htmlData,'lxml')
#print(data.prettify())
weather = data.find('div',attrs={'class':"wea_weather clearfix"})
#print(weather)
temp1=weather.find('em').get_text()
temp2=weather.find('b').get_text()
#使用select标签时 如果class中有空格 将空格改为"."才能筛选出来
AQI = data.select(".wea_alert.clearfix > ul > li > a > em")[0].get_text()
H=data.select(".wea_about.clearfix > span")[0].get_text() #湿度
S=data.select(".wea_about.clearfix > em")[0].get_text() #风速
if city_pin=='beijing' or city_pin=='tianjin':
B=data.select(".wea_about.clearfix > b")[0].get_text() #限行 只有北京和天津才有
A=data.select(".wea_tips.clearfix > em")[0].get_text() #今日天气提示
C=list(data.select(".live_index_grid > ul > li")[1].get_text().strip())
C=C[0]+C[1]
date=str(datetime.date.today()) #获取当天日期
if city=='北京' or city=='天津':
info='来自熊熊和闪闪的贴心提示\n'+city+'市'+','+date+'\n'+'实时温度: '+temp1+'℃'+','+ temp2 + '\n' '湿度:' + H + '\n' '风速:' + S + '\n' '紫外线:' + C +'\n' '今日提示:' + A + '\n' +'今日限行:' + B
elif city=='上海' or city=='重庆':
info='来自熊熊和闪闪的贴心提示\n'+city+'市'+','+date+'\n'+'实时温度: '+temp1+'℃'+','+ temp2 + '\n' '湿度:' + H + '\n' '风速:' + S + '\n' '紫外线:' + C +'\n' '今日提示:' + A
else:
info='来自熊熊和闪闪的贴心问候\n'+prov+'省'+city+'市'+','+date+'\n'+'实时温度: '+temp1+'℃'+','+ temp2 + '\n' '湿度:' + H + '\n' '风速:' + S + '\n' '紫外线:' + C +'\n' '今日提示:' + A
#获取明日天气
tom=data.select(".days.clearfix ")[1].find_all('li')
#print(tom)
temp_t=tom[2].get_text().replace('°','℃')+','+tom[1].find('img').attrs['alt'] #明天温度
S_t1=tom[3].find('em').get_text()
S_t2=tom[3].find('b').get_text()
S_t=S_t1+S_t2 #明日风速
A_t=tom[-1].get_text().strip() #明日空气质量
info_t = '\n明日天气:\n' + '温度:' + temp_t + '\n' + '风速:' + S_t + '\n' '空气质量:' + A_t + '\n'
#定义一个tips的字典
tips_dict = {'cold':'感冒预测','makeup':'化妆指数','uray':'紫外线量','dress':'穿衣指数','car':'关于洗车','sport':'运动事宜'}
info_tips=''
#print(list(tips_dict.keys()))
for i in list(tips_dict.keys()):
url_tips=url.replace('weather',i)
htmlData=request.urlopen(url_tips).read().decode('utf8')
data=BeautifulSoup(htmlData,'lxml')
tips=data.select(".aqi_info_tips > dd")[0].get_text()
info_tips=info_tips+tips_dict.get(i)+':'+tips+'\n'`
链接微信时注意 需要安装另外一个模块itchat模块
#链接微信
itchat.auto_login(hotReload=True)#在一段时间内运行不需要扫二维码登录
global info_all
info_all=info + '\n' + info_tips + info_t
#发送给微信个人
def sendToPerson(nickName):
user=itchat.search_friends(name=nickName) # 使用备注名或者昵称搜索,微信号不行;若有重名的则全部返回,列表
userName=user[0]['UserName']
itchat.send(info_all,toUserName=userName)
print('succeed')
#发送微信群
def sendToRoom(nickName):
#print(itchat.get_chatrooms())
print(itchat.search_chatrooms(name=nickName))# 支持模糊匹配
user=itchat.search_chatrooms(name=nickName)
print(user)
userName = user[0]['UserName']
itchat.send(info_all,toUserName=userName)
print('succeed')
if __name__=='__main__':
choice()
sendToPerson(input("你想要关心哪个小可爱呀?"))
sendToRoom(input("你要问候哪个群呀?"))
实验结果如下
全部代码如下
import itchat
from urllib import request
import datetime
from bs4 import BeautifulSoup
from xpinyin import Pinyin
pin = Pinyin()
def choice():
print('请输入 1 代表直辖市 2 代表非直辖市')
num=input()
if num=='1':
city=input("请输入城市:")
city_pin=pin.get_pinyin(city,'')
get_info(None,city_pin,None,city)
else:
prov=input("请输入省份:")
city=input("请输入城市:")
if prov=='陕西':
prov_pin='shaanxi'
city_pin=pin.get_pinyin(city,'')
get_info(prov_pin,city_pin,prov,city)
else:
prov_pin=pin.get_pinyin(prov,'') #将汉字转# 为拼音
city_pin=pin.get_pinyin(city,'')
#print(prov_pin,city_pin)
get_info(prov_pin,city_pin,prov,city)
def get_info(prov_pin,city_pin,prov,city):
if prov_pin==None:
url='https://tianqi.moji.com/weather/china/'
url=url+city_pin+'/'+city_pin
else:
url='https://tianqi.moji.com/weather/china/'
url=url+prov_pin+'/'+city_pin
print(url)
#获取天气信息begin#
htmlData=request.urlopen(url).read().decode('utf8')
data=BeautifulSoup(htmlData,'lxml')
#print(data.prettify())
weather = data.find('div',attrs={'class':"wea_weather clearfix"})
#print(weather)
temp1=weather.find('em').get_text()
temp2=weather.find('b').get_text()
#使用select标签时 如果class中有空格 将空格改为"."才能筛选出来
AQI = data.select(".wea_alert.clearfix > ul > li > a > em")[0].get_text()
H=data.select(".wea_about.clearfix > span")[0].get_text() #湿度
S=data.select(".wea_about.clearfix > em")[0].get_text() #风速
if city_pin=='beijing' or city_pin=='tianjin':
B=data.select(".wea_about.clearfix > b")[0].get_text() #限行 只有北京和天津才有
A=data.select(".wea_tips.clearfix > em")[0].get_text() #今日天气提示
C=list(data.select(".live_index_grid > ul > li")[1].get_text().strip())
C=C[0]+C[1]
date=str(datetime.date.today()) #获取当天日期
if city=='北京' or city=='天津':
info='来自熊熊和闪闪的贴心提示\n'+city+'市'+','+date+'\n'+'实时温度: '+temp1+'℃'+','+ temp2 + '\n' '湿度:' + H + '\n' '风速:' + S + '\n' '紫外线:' + C +'\n' '今日提示:' + A + '\n' +'今日限行:' + B
elif city=='上海' or city=='重庆':
info='来自熊熊和闪闪的贴心提示\n'+city+'市'+','+date+'\n'+'实时温度: '+temp1+'℃'+','+ temp2 + '\n' '湿度:' + H + '\n' '风速:' + S + '\n' '紫外线:' + C +'\n' '今日提示:' + A
else:
info='来自熊熊和闪闪的贴心问候\n'+prov+'省'+city+'市'+','+date+'\n'+'实时温度: '+temp1+'℃'+','+ temp2 + '\n' '湿度:' + H + '\n' '风速:' + S + '\n' '紫外线:' + C +'\n' '今日提示:' + A
#获取明日天气
tom=data.select(".days.clearfix ")[1].find_all('li')
#print(tom)
temp_t=tom[2].get_text().replace('°','℃')+','+tom[1].find('img').attrs['alt'] #明天温度
S_t1=tom[3].find('em').get_text()
S_t2=tom[3].find('b').get_text()
S_t=S_t1+S_t2 #明日风速
A_t=tom[-1].get_text().strip() #明日空气质量
info_t = '\n明日天气:\n' + '温度:' + temp_t + '\n' + '风速:' + S_t + '\n' '空气质量:' + A_t + '\n'
#定义一个tips的字典
tips_dict = {'cold':'感冒预测','makeup':'化妆指数','uray':'紫外线量','dress':'穿衣指数','car':'关于洗车','sport':'运动事宜'}
info_tips=''
#print(list(tips_dict.keys()))
for i in list(tips_dict.keys()):
url_tips=url.replace('weather',i)
htmlData=request.urlopen(url_tips).read().decode('utf8')
data=BeautifulSoup(htmlData,'lxml')
tips=data.select(".aqi_info_tips > dd")[0].get_text()
info_tips=info_tips+tips_dict.get(i)+':'+tips+'\n'
#链接微信
itchat.auto_login(hotReload=True)#在一段时间内运行不需要扫二维码登录
global info_all
info_all=info + '\n' + info_tips + info_t
#发送给微信个人
def sendToPerson(nickName):
user=itchat.search_friends(name=nickName) # 使用备注名或者昵称搜索,微信号不行;若有重名的则全部返回,列表
userName=user[0]['UserName']
itchat.send(info_all,toUserName=userName)
print('succeed')
#发送微信群
def sendToRoom(nickName):
#print(itchat.get_chatrooms())
print(itchat.search_chatrooms(name=nickName))# 支持模糊匹配
user=itchat.search_chatrooms(name=nickName)
print(user)
userName = user[0]['UserName']
itchat.send(info_all,toUserName=userName)
print('succeed')
if __name__=='__main__':
choice()
sendToPerson(input("你想要关心哪个小可爱呀?"))
sendToRoom(input("你要问候哪个群呀?"))
本文参考https://www.cnblogs.com/aby321/p/9633462.html
版权声明:本文为dd205qq原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。