simple_html_dom来抓取整个网站

时间:2014-06-07 13:06:05

标签: parsing simple-html-dom web-crawler

我想抓取整个网站。我使用Simple_html_dom进行解析,但问题是它一次只需要一个网页链接。我想只提供开始(主页)链接,它应该自动抓取并解析该网站的所有网页。有任何建议怎么做?

1 个答案:

答案 0 :(得分:2)

解析该单个页面的DOM时,将所有链接(在同一个域中)存储在一个数组中。然后,在解析结束时,检查数组是否为空。如果不是,请取第一个链接并执行相同的操作。

类似的东西(用类似Python语法编写的代码示例,但你可以很容易地将它改编为PHP - 我的生锈了)。

referenced_links = ['your_initial_page.html']

while referenced_links:  # if the array isn't empty...
    crawl_dom(referenced_links[0])
    referenced_links.pop(0)  # remove the first item in that array

def crawl_dom(url):
    # download the url, parse the DOM and append all hyperlinks to the array referenced_links