【问题标题】:how to get all the urls of a website using a crawler or a scraper?如何使用爬虫或刮刀获取网站的所有网址?
【发布时间】:2013-07-31 08:33:55
【问题描述】:

我必须从网站上获取许多 url,然后我必须将它们复制到一个 excel 文件中。 我正在寻找一种自动的方法来做到这一点。该网站的结构有一个包含大约 300 个链接的主页,每个链接内部都有 2 或 3 个对我来说很有趣的链接。 有什么建议吗?

【问题讨论】:

  • 您是否对编程感兴趣,如果有,是什么语言?
  • 是的。实际上任何语言,但我更喜欢 Python

标签: python url web-crawler scraper


【解决方案1】:

如果你想用 Python 开发你的解决方案,那么我可以推荐 Scrapy 框架。

至于将数据插入 Excel 工作表,有一些方法可以直接进行,例如:Insert row into Excel spreadsheet using openpyxl in Python,但您也可以将数据写入 CSV 文件,然后将其导入 Excel .

【讨论】:

    【解决方案2】:

    如果链接在 html 中...您可以使用漂亮的汤。这在过去对我有用。

    import urllib2
    from bs4 import BeautifulSoup
    
    page = 'http://yourUrl.com'
    opened = urllib2.urlopen(page)
    soup = BeautifulSoup(opened)
    
    for link in soup.find_all('a'):
        print (link.get('href'))
    

    【讨论】:

      【解决方案3】:

      你试过 selenium 或 urllib 吗?.urllib 比 selenium 快 http://useful-snippets.blogspot.in/2012/02/simple-website-crawler-with-selenium.html

      【讨论】:

        【解决方案4】:

        你可以用漂亮的汤来解析, [http://www.crummy.com/software/BeautifulSoup/]

        更多关于文档的信息在这里http://www.crummy.com/software/BeautifulSoup/bs4/doc/

        我不建议使用 scrappy,因为您在问题中描述的工作不需要它。

        例如此代码将使用 urllib2 库打开一个 google 主页并以列表的形式在该输出中查找所有链接

        import urllib2
        from bs4 import BeautifulSoup
        
        data=urllib2.urlopen('http://www.google.com').read()
        soup=BeautifulSoup(data)
        print soup.find_all('a')
        

        要处理 excel 文件,请查看 http://www.python-excel.org

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-31
          • 2012-09-24
          • 2013-11-23
          • 2012-02-09
          • 2019-10-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多