为了抓 取网站 ,我们首先需要下载包含有感兴趣数据的网页,该过程一般 被称为爬取(crawling)。 爬取 一个网站 有 很多种方法,而选用哪种方法更加 合适,则取决于目标网站 的结构 。 首先会探讨如何安全地下载网页, 然后会介绍如下爬取网站 的 常见方法:
·爬取网站 地图1
·遍历每个网页的 数据库ID
· 跟踪网页链接 。
下载 网 页
要想爬取网页,我们首先需要将其下载下来。 下面的示例脚 本使用Python
的 urllib2 模块下载URL。
下面是爬去站点地图的脚本 python写的
上代码吧。废话不多说
# -*- coding: utf-8 -*-
import re
from common import download
def crawl_sitemap(url):
# download the sitemap file
sitemap = download(url)
# extract the sitemap links
links = re.findall('(.*?)', sitemap)
# download each link
for link in links:
html = download(link)
# scrape html here
# ...
if __name__ == '__main__':
Tag标签: