这些年,我穿过的那些队服
在ipython notebook中调用ggplot的三种不同的方法

在Mac OS X上构建wget来抓取静态网站内容

posted @ 2014年6月29日 22:32 in 未分类 with tags mac , 5517 阅读
分享到: 更多

我们的目标是在Mac OS上获取一个静态服务器的内容,通常用wget是一个很好的选择。

wget是一个命令行工具用于从网络服务器来获取内容。但是在Mac OS X(Mountain Lion/ Mavericks / Snow Leopard)上没有提供该工具,但是有curl。

wget VS curl

curl

  • 基于跨平台的库libcurl

  • 支持unix管道

  • 返回错误代码来支持错误处理

  • 只返回单个url的内容,不支持自动取链接的内容

  • 大量协议支持诸如 :FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS, FILE, POP3, IMAP, SMTP, RTMP and RTSP

  • 可移植性好

  • 支持不同的SSL/TSL库

  • 支持HTTP认证(HTTP Authentication)

  • 支持双向和多部分提交数据

  • 支持压缩

  • MIT协议

wget

  • 只支持命令行

  • 支持递归的抓取数据,也就是说可以抓取返回内容中的url链接的内容。

  • 非常古老,开发不活跃。

  • 使用HTTP 1.0

  • GNU项目的一部分

  • GPL 协议

总体而言curl比wget要进步许多,可是要获取一个网站的镜像,迭代功能必不可少。只好自己动手,在Mac上构建一个wget。

构建wget

首先确定你已经安装了Xcode和GCC,如果不知道如何安装,可以参考这个链接

然后从gnu下载wget的源码

curl -O http://ftp.gnu.org/gnu/wget/wget-1.15.tar.gz

下载好后,解压缩

tar -xvf wget-1.15.tar.gz

解压缩好后,需要运行配置命令,为编译做准备

cd wget-1.15
./configure --with-ssl=openssl

这里我们选用openssl作为ssl的参数选项。大家一定不会忘记最近发生的openssl的heartbleed漏洞吧 :)

配置好了以后,运行make

make

这里不出意外会跳出一大堆的警告,不要担心,如果你看到如下的内容,你应该编译成功了

... ...
... ...
gcc  -O2 -Wall   -o wget cmpt.o connect.o convert.o cookies.o ftp.o css_.o css-url.o ftp-basic.o ftp-ls.o hash.o host.o html-parse.o html-url.o http.o init.o log.o main.o netrc.o progress.o ptimer.o recur.o res.o retr.o spider.o url.o warc.o utils.o exits.o build_info.o  version.o ftp-opie.o openssl.o http-ntlm.o ../lib/libgnu.a -liconv  -lssl -lcrypto -lz -ldl -lz -lz
Making all in doc
./texi2pod.pl -D VERSION="1.15" ./wget.texi wget.pod
/usr/bin/pod2man --center="GNU Wget" --release="GNU Wget 1.14" wget.pod > wget.1
Making all in po
Making all in tests
make[2]: Nothing to be done for `all'.
Making all in util
make[2]: Nothing to be done for `all'.
make[2]: Nothing to be done for `all-am'.

最后,安装

sudo make install

安装成功后,试一试wget是否成功安装

$ which wget
/usr/local/bin/wget

如果看到上述结果说明wget已经成功构建并部署到/usr/local/bin目录了

好了,万事具备,可以开始抓取你想要获得内容的网站了。

wget -mk http://website.com

其中-m参数表示迭代的抓取,-k参数表示用相对路径取代绝对路径。抓取的内容会被存放在本地的website.com的目录下。

举个例子,比如我要抓新浪新闻

$ wget -mk http://news.sina.com.cn
--2014-06-30 16:55:26--  http://news.sina.com.cn/
Resolving news.sina.com.cn... 58.63.236.31, 58.63.236.46, 58.63.236.48, ...
Connecting to news.sina.com.cn|58.63.236.31|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 636992 (622K) [text/html]
Saving to: ‘news.sina.com.cn/index.html’

100%[======================================>] 636,992      391KB/s   in 1.6s   

2014-06-30 16:55:29 (391 KB/s) - ‘news.sina.com.cn/index.html’ saved [636992/636992]

Loading robots.txt; please ignore errors.
--2014-06-30 16:55:29--  http://news.sina.com.cn/robots.txt
Reusing existing connection to news.sina.com.cn:80.
HTTP request sent, awaiting response... 200 OK
Length: 70 [text/plain]
Saving to: ‘news.sina.com.cn/robots.txt’

100%[======================================>] 70          --.-K/s   in 0.03s   

2014-06-30 16:55:29 (2.54 KB/s) - ‘news.sina.com.cn/robots.txt’ saved [70/70]

--2014-06-30 16:55:29--  http://news.sina.com.cn/js/792/2012-08-09/41/headnews.js
Reusing existing connection to news.sina.com.cn:80.
HTTP request sent, awaiting response... 200 OK
Length: 31699 (31K) [application/x-javascript]
Saving to: ‘news.sina.com.cn/js/792/2012-08-09/41/headnews.js’

100%[======================================>] 31,699      --.-K/s   in 0.04s   

2014-06-30 16:55:29 (731 KB/s) - ‘news.sina.com.cn/js/792/2012-08-09/41/headnews.js’ saved [31699/31699]

--2014-06-30 16:55:29--  http://news.sina.com.cn/pfpnews/js/libweb.js
Reusing existing connection to news.sina.com.cn:80.
HTTP request sent, awaiting response... 200 OK
Length: 6554 (6.4K) [application/x-javascript]
Saving to: ‘news.sina.com.cn/pfpnews/js/libweb.js’

100%[======================================>] 6,554       --.-K/s   in 0.03s

抓成功后的目录如下

注意:

  • 这样的方式仅适用于静态网站,对于使用动态代码生成的网站无能为力

  • 地址转换是发生在所有内容抓取完成之后,如果你中途退出,则所有的地址链接仍然指向原始地址。当你所要抓取的内容巨大时,需要非常小心。

  • 为了防止流量过大,对服务器造成太大的负担,可以使用-w参数设置两个请求中的间隔时间



分享到: 更多
Avatar_small
mjshi 说:
2014年10月15日 22:39

试试这个:http://brew.sh/。

Avatar_small
mega888 apk download 说:
2021年8月29日 05:42

Mmm.. estimable to be here in your report or notify, whatever, I repute I should moreover process strong for my have website want I play some salubrious further updated busy in your location.

Avatar_small
kaleem 说:
2021年11月28日 01:41

Many people are interested to choose bitcoin. Many people lost it. If you lose it don't worry. Because you receive your currency by Bitcoin private key recovery tool. You read more options by clicking here. https://primeelectrolite.com

Avatar_small
milan 说:
2023年1月11日 18:34

Building wget on Mac OS X is a great way to scrape static website content. By following a few buy cheap diamond rings simple steps, you can have a powerful tool that can help you gather data from any website. Plus, building wget on Mac OS X is free and easy to do.

Avatar_small
seo service london 说:
2024年1月13日 03:35

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your post


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter