【问题标题】:Python: listing all the URLs of a website without indexPython:列出没有索引的网站的所有 URL
【发布时间】:2016-06-15 14:26:53
【问题描述】:

我可以单独访问以下每个 URL:http://www.example.com/{.*}.html

但是,对主页 http://www.example.com 的访问受到某种限制,我被重定向到显示错误页面:Erreur 403 - Refus de traitement de la requête (Interdit - Forbidden)

有没有办法列出该域下托管的 HTML 页面的所有 URL?

【问题讨论】:

    标签: python url web-crawler


    【解决方案1】:

    简短的回答是否定的。您不能像列出目录一样仅列出该域中的所有 HTML 页面。假设网站的 robots.txt 允许,最好的办法是使用网络爬取模块来爬取网站,例如 http://scrapy.org/

    【讨论】:

    • 我尝试使用scrapy,但无法废弃任何项目。收到以下消息:[scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023 [scrapy] DEBUG: Crawled (403) <GET http://www.example.com> (referer: None) ['partial'] [scrapy] DEBUG: Ignoring response <403 http://www.example.com>: HTTP status code is not handled or not allowed
    • 您无法访问被禁止的站点(错误 403)。您需要从example.com/index.html 或其他有很多链接的站点开始。爬取会递归到网站上的所有链接,但仍然无法访问被禁止的网站。
    【解决方案2】:

    感谢 Brian:我设法从域下托管的可访问 HTML 页面列表开始抓取。

    # scrap.py
    
    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    
    class MySpider(CrawlSpider):
        name = 'example.com'
        allowed_domains = ['example.com']
        start_urls = [
            'http://www.example.com/***.html'  # Accessible URL
        ]
    
        rules = (
            Rule(LinkExtractor(allow=('\.html', )), callback='parse_item', follow=True),
        )
    
        def parse_item(self, response):
            print response.url
    

    然后:

    $ scrapy runspider scrap.py > urls.out
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多