简单的python web-crawler

时间:2016-03-09 03:18:52

标签: python url web-crawler

我一直在寻找一个简单的,工作的和网络爬虫(也称为蜘蛛)并且找不到它。任何人都可以帮我解决这个问题吗?我希望它只是从指定的URL获取所有链接以及它扫描的每个URL的所有链接。

3 个答案:

答案 0 :(得分:0)

您可以使用scrapy模块。

或者,您可以编写自己的抓取工具,使用模块来获取数据(即request,urllib2或selenium)和一些HTML解析器(BeautifulSoup或selenium的内置解析器)。

答案 1 :(得分:0)

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

我不知道任何模块会简单地为你取得所有链接,所以你必须自己完成这个过程。

首先,从您那里获取HTML链接urllib2。然后,解析HTML并找到BeautifulSoup的链接。页面上甚至还有一节介绍如何从网页上获取所有链接。

这真是所有“困难”的代码。然后,您可以将所有链接附加到列表中,浏览每个链接,重复上述相同的过程,然后再将结果链接添加到列表中,并按照您想要的时间递归重复此过程。这应该是一个基本的网络爬虫。

答案 2 :(得分:0)

您可以将其用作网络抓取工具,但我不确定它是否可行,因为它会给我一些错误,但您可能安装了另一个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)