爬蟲下載器
此下載器主要作用是從url管理器中獲取新的url並將其從對應服務器中下載下來。
實現代碼
這裏的代碼與爬蟲學習(一)中的互通。
可使用urllib庫,或者request庫。
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
__author__ = 'Gary'
# 爬蟲下載器
import urllib.request
class HtmlDownloader(object):
def download(self, url):
if url is None:
return None
response = urllib.request.urlopen(url)
if response.getcode() != 200:
return None
return response.read()
# # 使用request庫
# import requests
#
#
# class HtmlDownloader(object):
# def download(self, url):
# if url is None:
# return None
#
# r = requests.get(url)
#
# if r.status_code !=200:
# return None
#
# return r.text