微信小程序

#python开发两种形式
#1.itchat
#2.wxpy 对itchat做进一步优化
import itchat
from itchat.content import TEXT
import requests
import re
import json
import requests
api_k = '278ea7d0b4bc4dfe920359a54b294c10'
json_data = {
             "reqType": 0,
             "perception": {
                 "inputText": {
                     "text": "我叫什么?"
                 },

             },
             "userInfo": {
                 "apiKey": api_k,
                 "userId": "hello"
             }
         }

pattern = re.compile(r"1[34578]\d{9}")
#TEXT接受消息类型
@itchat.msg_register(TEXT, isFriendChat=True, isGroupChat=True, isMpChat=True)
def simple_reply(msg):
    print(msg.text)

    phone = msg.text
    result = pattern.match(phone)
    if not result:
      print("手机号格式错误!请重新输入!")
      city = msg.text
      if city in ['郑州','开封','信阳']:
             api = f'http://api.map.baidu.com/telematics/v3/weather?location={city}&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
             response = requests.get(api)
             # text:普通文本  content:二进制文本
             json_content = response.text

             # 解析json数据

             weather = json.loads(json_content)
             return weather['results'][0]['weather_data'][0]['temperature']

      else:
             url = 'http://openapi.tuling123.com/openapi/api/v2'
             json_data["perception"]["inputText"]["text"] = city
             response = requests.post(url, json=json_data)
             html_content = response.text
             html_content = json.loads(html_content)
             html_content = html_content["results"][0]["values"]["text"]

             return (html_content)

    if result:
      api = f'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={phone}'
      response = requests.get(api)
      other_content = response.text
      other_content = other_content.replace('__GetZoneResult_ = ', '')
      place = other_content.split(",")[-1].split(':')[-1]
      place = place.strip("\n }'")
   # print(place)
      return  place
itchat.auto_login(True)
itchat.run()


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值