用ESP8266获取网页信息+获取b站粉丝数案例

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个使用ESP8266连接B站API获取B站粉丝的示例代码: ``` #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> const char* ssid = "Your_SSID"; // 替换为你的Wi-Fi网络名称 const char* password = "Your_PASSWORD"; // 替换为你的Wi-Fi网络密码 const char* host = "api.bilibili.com"; const int httpsPort = 443; String path = "/x/relation/stat?vmid=Your_UID"; // 替换为你的B站用户UID void setup() { Serial.begin(115200); delay(10); // 连接Wi-Fi网络 Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { WiFiClientSecure client; Serial.print("Connecting to "); Serial.println(host); if (!client.connect(host, httpsPort)) { Serial.println("Connection failed"); return; } String url = "https://" + String(host) + path; Serial.print("Requesting URL: "); Serial.println(url); client.print(String("GET ") + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP8266\r\n" + "Connection: close\r\n\r\n"); Serial.println("Request sent"); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { Serial.println("Headers received"); break; } } String response = client.readStringUntil('\n'); Serial.print("Response: "); Serial.println(response); // 解析JSON响应 int index = response.indexOf("follower"); String followers = response.substring(index + 10, index + 16); followers.replace(",", ""); Serial.print("Followers: "); Serial.println(followers); delay(5000); // 5秒更新一次 } ``` 注意,这个程序需要先安装WiFiClientSecure库和ArduinoJson库。此外,你需要替换代码中的Wi-Fi网络名称、Wi-Fi网络密码、B站用户UID等参。在程序运行时,它会连接到Wi-Fi网络,然后通过HTTPS连接到B站API获取用户的粉丝,并将其显示在串口监视器中。你可以根据需要修改程序来显示其他信息或将结果发送到其他设备。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

创客阿蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值