首页 文章

simple_html_dom抓取整个网站[关闭]

提问于
浏览
0

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

1 回答

  • 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
    

相关问题