- 首先下载ESP8266下载工具点击下载烧录固件工具地址
- 点击下载固件
- 将ESP8266插入电脑中,打开工具 ,选择
然后选择烧录好的固件,先擦除
然后地址选为0x0000开始,点击烧录
- 然后下载上传下载文件的工具,uPyCraft下载以及使用
下载完成后,首先打开软件,点击连接串口,进入交互模式REPL
有>>>
这个符号,表示成功进入Python环境
在下面的窗口可以输入print('hello world!')
就可以进入今天的主题了!
import urequests
import json
import network
#连接WiFi
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():print('connecting to network...')sta_if.active(True)sta_if.connect('!!wifiname', '!!passwd')while not sta_if.isconnected():pass
print('network config:', sta_if.ifconfig())
#get请求的头部信息
head = {
"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}
#B站获取用户信息的URL
url = 'https://api.bilibili.com/x/relation/stat?vmid=!!yourid&jsonp=jsonp'
#获取信息
r = urequests.get(url,headers = head)
#将信息中需要的部分弄出来
datadict = json.loads(r.text)['data']
print('follower is:' + str(datadict['follower']))
print('following is:' + str(datadict['following']))
#以下是具体返回回来的信息
#{"code":0,"message":"0","ttl":1,"data":{"mid":429xxxx,"following":59,"whisper":0,"black":0,"follower":21}}
注意代码中!!两个感叹号和后面的信息,需要换为你自己的信息,比如!!wifiname,要换位你自己的wifi名字
代码解读
首先是WiFi连接部分,这个自己看手册就OK了,很简单
然后是后面url,这个URL是b站的一个api
https://api.bilibili.com/x/relation/stat?vmid=!!yourid&jsonp=jsonp
,其中!!yourid是你访问b站的时候,自己网页上后面的那部分,比如
也就是429xxxx这部分,可以测试一下,比如我放一个
返回的就是一个json字符串换
然后通过json.loads函数可以将json格式的字符串变为字典,后面就可以轻松输出了!
将以上代码复制到代码区
然后点击
下面是micropython输出
这不是我的粉丝数,我随意找了一个up的id放上去的。…
注
如果没有urequests.py,将下面的文件命名为urequests.py,移入设备文件内
import usocketclass Response:def __init__(self, f):self.raw = fself.encoding = "utf-8"self._cached = Nonedef close(self):if self.raw:self.raw.close()self.raw = Noneself._cached = None@propertydef content(self):if self._cached is None:self._cached = self.raw.read()self.raw.close()self.raw = Nonereturn self._cached@propertydef text(self):return str(self.content, self.encoding)def json(self):import ujsonreturn ujson.loads(self.content)def request(method, url, data=None, json=None, headers={
}, stream=None,params=None):try:proto, dummy, host, path = url.split("/", 3)except ValueError:proto, dummy, host = url.split("/", 2)path = ""if proto == "http:":port = 80elif proto == "https:":import usslport = 443else:raise ValueError("Unsupported protocol: " + proto)if ":" in host:host, port = host.split(":", 1)port = int(port)if params:path = path + "?"for k in params:path = path + '&'+k+'='+params[k]ai = usocket.getaddrinfo(host, port)addr = ai[0][4]s = usocket.socket()s.connect(addr)if proto == "https:":s = ussl.wrap_socket(s)s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))if not "Host" in headers:s.write(b"Host: %s\r\n" % host)# Iterate over keys to avoid tuple allocfor k in headers:s.write(k)s.write(b": ")s.write(headers[k])s.write(b"\r\n")if json is not None:assert data is Noneimport ujsondata = ujson.dumps(json)if data:s.write(b"Content-Length: %d\r\n" % len(data))s.write(b"\r\n")if data:s.write(data)l = s.readline()protover, status, msg = l.split(None, 2)status = int(status)#print(protover, status, msg)while True:l = s.readline()if not l or l == b"\r\n":break#print(l)if l.startswith(b"Transfer-Encoding:"):if b"chunked" in l:raise ValueError("Unsupported " + l)elif l.startswith(b"Location:") and not 200 <= status <= 299:raise NotImplementedError("Redirects not yet supported")resp = Response(s)resp.status_code = statusresp.reason = msg.rstrip()return respdef head(url, **kw):return request("HEAD", url, **kw)def get(url, **kw):return request("GET", url, **kw)def post(url, **kw):return request("POST", url, **kw)def put(url, **kw):return request("PUT", url, **kw)def patch(url, **kw):return request("PATCH", url, **kw)def delete(url, **kw):return request("DELETE", url, **kw)