【问题标题】:Simple python web-crawler简单的python网络爬虫
【发布时间】:2016-06-23 06:36:06
【问题描述】:

我一直在寻找一种简单、有效的网络爬虫(也称为蜘蛛),但一直没有找到。谁能帮我解决这个问题?我希望它能够简单地获取来自指定 url 的所有链接以及来自它扫描的每个 url 的所有链接。

【问题讨论】:

    标签: python url web-crawler


    【解决方案1】:

    您可以使用scrapy 模块。

    或者,您可以编写自己的爬虫,使用用于获取数据的模块(即请求、urllib2 或 selenium)和一些 HTML 解析器(BeautifulSoup 或 selenium 的内置解析器)的组合。

    【讨论】:

      【解决方案2】:

      我以前没有尝试过制作网络爬虫。但是,我想它不应该太复杂。我会给你一些你可以使用的资源。

      我不知道有任何模块会简单地为您获取所有链接,因此您可能必须自己完成该过程。

      首先,从您那里获取带有urllib2 的链接的HTML。然后,解析 HTML 并找到带有BeautifulSoup 的链接。页面上甚至还有一个部分描述了如何从网页中获取所有链接。

      这真的是所有“困难”的代码。然后,您可以将获得的所有链接附加到列表中,遍历每个链接,重复与上述相同的过程,然后再次将生成的链接添加到列表中,并递归地重复此过程,无论您想要多长时间。这应该为您制作一个基本的网络爬虫。

      【讨论】:

        【解决方案3】:

        您可以将它用作网络爬虫,但我不确定它是否会工作,因为它会给我一些错误,但您可能安装了另一个 python 路径

        import requests
        from bs4 import BeautifulSoup
        
        
            def trade_spider(max_pages):
            page = 1
            while page <= max_pages:
            url = "https://buckysroom.org/trade/search.php?page=" + str(page)
            source_code = requests.get(url)
            # just get the code, no headers or anything
            plain_text = source_code.text
            # BeautifulSoup objects can be sorted through easy
            soup = BeautifulSoup(plain_text)
            for link in soup.findAll('a', {'class': 'item-name'}):
                href = "https://buckysroom.org" + link.get('href')
                title = link.string  # just the text, not the HTML
                print(href)
                print(title)
                # get_single_item_data(href)
            page += 1
        
        
        def get_single_item_data(item_url):
        source_code = requests.get(item_url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)
        # if you want to gather information from that page
        for item_name in soup.findAll('div', {'class': 'i-name'}):
            print(item_name.string)
         # if you want to gather links for a web crawler
         for link in soup.findAll('a'):
            href = "https://buckysroom.org" + link.get('href')
            print(href)
        
        
        trade_spider(1)
        

        【讨论】:

          猜你喜欢
          • 2017-01-26
          • 2023-03-13
          • 2021-12-06
          • 2016-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-26
          • 2015-05-12
          相关资源
          最近更新 更多