当前位置:   article > 正文

嵌入式linux学习笔记--使用wget下载文件_嵌入式无wget下载

嵌入式无wget下载

如何使用wget下载文件

CSDN 和百问网一起发

1.正确搭建开发环境

开发板直接接在路由器或者交换机下,确保这个交换机或者路由器能连接到需要下载的网络,本节会以下载百度的网页作为demo

所以需要 开发板能连接到外网。这一点很重要。

2.测试网络的连通性

2.1测试开发板到路由器的连接

这一步基本看一下网口灯就行了,正常的连接状态网口灯应该是亮起的。

2.2测试开发板能否连接外网

建议使用dhcp 的方式给开发板获得固定的IP ,笔者在野火的板子上进行测试的时候发现存在使用固定IP 的情况下缺少DNS 配置的问题。(笔者是自行写了配置脚本,开发板 可以运行脚本挂载 nfs 服务) 在百问网的板子上测试的时候发现使用udhcpc 获得动态IP的过程中直接附带了路由器的DNS 服务器信息。

使用固定IP 导致的缺少DNS 也可以通过修改配置文件解决 (执行以下语句 其中的223.5.5.5 为阿里云的DNS 服务)

echo "nameserver 223.5.5.5" >> /etc/resolv.conf
  • 1

默认板子重启之后的配置文件 /etc/resolv.conf 如下 (貌似也能ping到百度)

[root@imx6ull:~]# cat /etc/resolv.conf   # 板子上电之后的DNS配置信息
# Generated by Connection Manager
nameserver ::1
nameserver 127.0.0.1
[root@imx6ull:~]# udhcpc  # 获取动态IP 
udhcpc: started, v1.29.3
udhcpc: sending discover
udhcpc: sending select for 192.168.1.110
udhcpc: lease of 192.168.1.110 obtained, lease time 7200
deleting routers
adding dns 202.96.134.33
adding dns 202.96.128.166
[root@imx6ull:~]# cat /etc/resolv.conf #再次查看DNS 信息
# Generated by Connection Manager
nameserver ::1
nameserver 127.0.0.1
nameserver 202.96.134.33 # eth0
nameserver 202.96.128.166 # eth0
[root@imx6ull:~]#

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

综合起来就是 : 最好就是 开发板上电之后 运行 udhcpc 获得一个路由器分配的动态IP 。并且要能成功的ping通外网

[root@imx6ull:~]# ping www.baidu.com
PING www.baidu.com (14.215.177.38): 56 data bytes
64 bytes from 14.215.177.38: seq=0 ttl=56 time=7.638 ms
64 bytes from 14.215.177.38: seq=1 ttl=56 time=7.580 ms
  • 1
  • 2
  • 3
  • 4

2. 使用wget 获取资源

2.1获取网页资源

[root@imx6ull:~]# wget www.baidu.com
--2021-01-31 15:17:30--  http://www.baidu.com/
Resolving www.baidu.com... 14.215.177.39, 14.215.177.38
Connecting to www.baidu.com|14.215.177.39|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: 'index.html'

index.html          100%[===================>]   2.33K  --.-KB/s    in 0s

2021-01-31 15:17:30 (17.0 MB/s) - 'index.html' saved [2381/2381]

[root@imx6ull:~]# cat index.html
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[root@imx6ull:~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

如上述的片段显示的,通过wget 工具我们成功的获取了 百度的首页。

接下来获取一个图片 分析上面的html语言不难找到百度的首页图片,直接下载下来这个资源。

[root@imx6ull:~]# wget www.baidu.com/img/bd_logo1.png
--2021-01-31 15:19:46--  http://www.baidu.com/img/bd_logo1.png
Resolving www.baidu.com... 14.215.177.39, 14.215.177.38
Connecting to www.baidu.com|14.215.177.39|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7877 (7.7K) [image/png]
Saving to: 'bd_logo1.png'

bd_logo1.png        100%[===================>]   7.69K  --.-KB/s    in 0.004s

2021-01-31 15:19:46 (1.99 MB/s) - 'bd_logo1.png' saved [7877/7877]

[root@imx6ull:~]# ls
bd_logo1.png  index.html   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

如果想要将所有的资源都下载下来 需要加入 -p 参数

[root@imx6ull:~/temp]# wget -p www.baidu.com
--2021-01-31 15:23:25--  http://www.baidu.com/
Resolving www.baidu.com... 14.215.177.39, 14.215.177.38
Connecting to www.baidu.com|14.215.177.39|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: 'www.baidu.com/index.html'

www.baidu.com/index 100%[===================>]   2.33K  --.-KB/s    in 0s

2021-01-31 15:23:25 (18.1 MB/s) - 'www.baidu.com/index.html' saved [2381/2381]

Loading robots.txt; please ignore errors.
--2021-01-31 15:23:25--  http://www.baidu.com/robots.txt
Reusing existing connection to www.baidu.com:80.
HTTP request sent, awaiting response... 200 OK
Length: 2814 (2.7K) [text/plain]
Saving to: 'www.baidu.com/robots.txt'

www.baidu.com/robot 100%[===================>]   2.75K  --.-KB/s    in 0s

2021-01-31 15:23:25 (26.5 MB/s) - 'www.baidu.com/robots.txt' saved [2814/2814]

FINISHED --2021-01-31 15:23:25--
Total wall clock time: 0.06s
Downloaded: 2 files, 5.1K in 0s (21.8 MB/s)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

通过上述的代码 我们发现并没有把所有的文件都拿下来,可能是因为百度的反爬虫机制。不懂,估计也用不到

2.2获取服务器资源

这个需要提前知道服务器资源的位置

例如下载

wget http://mirrors.aliyun.com/ubuntu/project/ubuntu-archive-keyring.gpg.sig
wget https://gitee.com/weidongshan/openharmony_for_imx6ull.git --no-check-certificate    # git
wget https://atta.szlcsc.com/upload/public/pdf/source/20190802/C375844_D1F11326408C489F34467373E22B71B9.pdf  #立创商城的datesheet
wget http://wechatapppro-1252524126.file.myqcloud.com/appTVs2sJfo3933/image/3c196c4ca0f94877e848239b68e0e948.png  # 百问网的图标
wget https://cloud.firebbs.cn/portal/202005/18/173859n5cfr16h1u1ccczf.jpg      # 野火BBS 的封面

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号