11.利用esp8266爬取网页

代码:

#include<ESP8266WiFi.h>

//wifi参数
const char* ssid="wifi账户";
const char* password="WiFi密码";

const char*host="www.microsoft.com";

void setup() {
  // put your setup code here, to run once:
  //  启动串口
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to...");
  Serial.println(ssid);
  WiFi.begin(ssid,password);
  while(WiFi.status()!=WL_CONNECTED)   //连接WiFi
  {
    delay(500);

    Serial.print(",");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address");
  Serial.println(WiFi.localIP());   //打印开发板的ip地址
}

int value=0;

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("Connecting to ");
  Serial.println(host);

  //  使用WiFiclient类创建tcp连接
  WiFiClient client;
  const int httpPort=80;
  if(!client.connect(host,httpPort) )  //tcp连接失败
  {
    Serial.println("connection failed.");
    return;
  }
  
  //  向服务器发送一个request请求
  client.print(String("GET /")+"HTTP/1.1\r\n"+
  "Host:"+host+"\r\n"+"Connection:close\r\n\r\n");
  delay(5000);

  //  读取服务器返回的所用行,并打印到串口监视器
  while(client.available())
  {
    String line=client.readStringUntil('\r');
    Serial.print(line);
    delay(50);
  }
  Serial.println();
  Serial.println("closing conneation");
  delay(5000);
  

}

硬件连接:

硬件就是直接将esp8266开发板直接连接在电脑上即可。

点击上传,就可以直接在串口监视器中看到网页的源代码。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

豆豆豆豆芽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值