python01

import re
import urlparse
import urllib2
import time
from datetime import datetime
import robotparser
import Queue

#链接爬虫
'''
一个链接爬虫需要考虑以下几个问题:
1.网页可能不存在。就要用到tryexcept语句,捕获异常。
2.临时性的错误。比如服务器过载返回的503 Service Unavailable错误。就要多尝试几次下载。
3.代理。用户代理user_agent='wswp'
4.用正则表达式。'<a[^>]+href=["\'](.*?)["\']'.
5.相对链接问题。如果是相对链接就应该创建绝对链接。urlparse.urljoin()
6.重复爬取。爬取网页的时候,经常会出现将要爬取的网页中也有爬取过的链接,这样会造成不断循环。所以要建立一个URL管理器,管理爬取过的和未爬取的
7.爬虫协议(robots.txt),所以要引入robotparser模块,以避免下载禁止爬取的URL
8.用代理访问某个网站。
9.下载限速。如果我们爬取网站的速度过快,就会面临被封禁或者服务器过载的风险。所以应当在两次下载之间添加延时。delay
10.爬虫陷阱。最好设置一个爬取深度(max_depth)——记录到达当前网页经过了多少链接。
'''
def link_crawler(seed_url, link_regex=None, delay=5, max_depth=-1, max_urls=-1, headers=None, user_agent='wswp',
                 proxy=None, num_retries=1):
    """Crawl from the given seed URL following links matched by link_regex
    """
    # the queue of URL's that still need to be crawled 双向队列里面存储url
    crawl_queue = Queue.deque([seed_url])
    # the URL's that have been seen and at what depth
    seen = {seed_url: 0}
    # track how many URL's have been downloaded
    num_urls = 0
    rp = get_robots(seed_url)#获取robots.txt
    throttle = Throttle(delay)#下载限速
    headers = headers or {}
    if user_agent:
        headers['User-agent'] = user_agent#用户代理

    while crawl_queue:
        url = crawl_queue.pop()#移除列表中的元素,并且返回该元素的值
        # check url passes robots.txt restrictions
        if rp.can_fetch(user_agent, url):#确定指定的用户代理是否允许访问网页
            throttle.wait(url)#延迟
            html = download(url, headers, proxy=proxy, num_retries=num_retries)
            links = []

            depth = seen[url]
            if depth != max_depth:
                # can still crawl further
                if link_regex:
                    # filter for links matching our regular expression
                    links.extend(link for link in get_links(html) if re.match(link_regex, link))

                for link in links:
                    link = normalize(seed_url, link) #返回绝对链接
                    # check whether already crawled this link
                    if link not in seen:
                        seen[link] = depth + 1
                        # check link is within same domain
                        if same_domain(seed_url, link):
                            # success! add this new link to queue
                            crawl_queue.append(link)

            # check whether have reached downloaded maximum
            num_urls += 1
            if num_urls == max_urls:
                break
        else:
            print 'Blocked by robots.txt:', url

#下载限速
class Throttle:
    """Throttle downloading by sleeping between requests to same domain
    """

    def __init__(self, delay):
        # amount of delay between downloads for each domain
        self.delay = delay
        # timestamp of when a domain was last accessed
        self.domains = {}

    def wait(self, url):
        domain = urlparse.urlparse(url).netloc#服务器位置
        last_accessed = self.domains.get(domain)

        if self.delay > 0 and last_accessed is not None:
            sleep_secs = self.delay - (datetime.now() - last_accessed).seconds
            if sleep_secs > 0:
                time.sleep(sleep_secs)
        self.domains[domain] = datetime.now()


def download(url, headers, proxy, num_retries, data=None):
    print 'Downloading:', url
    request = urllib2.Request(url, data, headers)
    opener = urllib2.build_opener()
    if proxy:
        proxy_params = {urlparse.urlparse(url).scheme: proxy}
        opener.add_handler(urllib2.ProxyHandler(proxy_params))
    try:
        response = opener.open(request)
        html = response.read()
        code = response.code
    except urllib2.URLError as e:
        print 'Download error:', e.reason
        html = ''
        if hasattr(e, 'code'):
            code = e.code
            if num_retries > 0 and 500 <= code < 600:
                # retry 5XX HTTP errors
                return download(url, headers, proxy, num_retries - 1, data)
        else:
            code = None
    return html


def normalize(seed_url, link):
    """Normalize this URL by removing hash and adding domain
    """
    link, _ = urlparse.urldefrag(link)  # remove hash to avoid duplicates urldefrag(url)url分解成去掉fragment的新url和去掉的fragment的二元组
    return urlparse.urljoin(seed_url, link)#绝对链接


def same_domain(url1, url2):
    """Return True if both URL's belong to same domain
    """
    #url分解成部件的6元组
    return urlparse.urlparse(url1).netloc == urlparse.urlparse(url2).netloc


def get_robots(url):
    """Initialize robots parser for this domain
    """
    rp = robotparser.RobotFileParser()
    rp.set_url(urlparse.urljoin(url, '/robots.txt'))#绝对链接
    rp.read()
    return rp


def get_links(html):
    """Return a list of links from html
    """
    # a regular expression to extract all links from the webpage
    #re.compile()函数将正则表达式的字符串形式编译为Pattern实例,然后使用Pattern实例处理文本并获得匹配结果(一个Match实例),最后使用Match实例获得信息,进行其他的操作。
    webpage_regex = re.compile('<a[^>]+href=["\'](.*?)["\']', re.IGNORECASE)
    # list of all links from the webpage
    return webpage_regex.findall(html)


if __name__ == '__main__':
    link_crawler('http://example.webscraping.com', '/(index|view)', delay=0, num_retries=1, user_agent='BadCrawler')
    link_crawler('http://example.webscraping.com', '/(index|view)', delay=0, num_retries=1, max_depth=1,
                 user_agent='GoodCrawler')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值