How to download files using wget

来自百问网嵌入式Linux wiki

如何使用wget下载文件

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


默认板子重启之后的配置文件 /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:~]#

综合起来就是 : 最好就是 开发板上电之后 运行 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

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]

如上述的片段显示的,通过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   

如果想要将所有的资源都下载下来 需要加入 -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)

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

2.2获取服务器资源

这个需要提前知道服务器资源的位置,以下是我测试的时候的用例时间(2021-01-30 以及2021-02-04) 例如下载

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 的封面