1. 程式人生 > >磁碟快取

磁碟快取

#-*- coding:UTF-8 -*-
#原來建立物件時或者是呼叫類以外的方法時提示沒有定義是因為這些類或方法的位置不應該放在主函式後面,而應該放在主函式前面
import urlparse
import urllib2
import random
import time
from datetime import datetime, timedelta
import socket
import robotparser
import csv
import re
import lxml.html
DEFAULT_AGENT = 'wswp'
DEFAULT_DELAY = 5
DEFAULT_RETRIES = 1
DEFAULT_TIMEOUT = 60 def link_crawler(seed_url, link_regex=None, max_depth=-1, max_urls=-1, user_agent='wswp', scrape_callback=None, cache=None): """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 = [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 D = Downloader(DEFAULT_DELAY, DEFAULT_AGENT, None, DEFAULT_RETRIES, DEFAULT_TIMEOUT, None, None) while crawl_queue: url = crawl_queue.pop()#移除列表中的元素,並且返回該元素的值 depth = seen[url] # check url passes robots.txt restrictions
if rp.can_fetch(user_agent, url):#確定指定的使用者代理是否允許訪問網頁 html = D(url) links = [] if scrape_callback: links.extend(scrape_callback(url, html) or []) 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 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 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_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) class Downloader: def __init__(self, delay=DEFAULT_DELAY, user_agent=DEFAULT_AGENT, proxies=None, num_retries=DEFAULT_RETRIES, timeout=DEFAULT_TIMEOUT, opener=None, cache=None): socket.setdefaulttimeout(timeout) self.throttle = Throttle(delay) self.user_agent = user_agent self.proxies = proxies self.num_retries = num_retries self.opener = opener self.cache = cache def __call__(self, url): result = None if self.cache: try: result = self.cache[url] except KeyError: # url is not available in cache pass else: if self.num_retries > 0 and 500 <= result['code'] < 600: # server error so ignore result from cache and re-download result = None if result is None: # result was not loaded from cache so still need to download self.throttle.wait(url) proxy = random.choice(self.proxies) if self.proxies else None headers = {'User-agent': self.user_agent} result = self.download(url, headers, proxy=proxy, num_retries=self.num_retries) if self.cache: # save result to cache self.cache[url] = result return result['html'] def download(self, url, headers, proxy, num_retries, data=None): print 'Downloading:', url request = urllib2.Request(url, data, headers or {}) opener = self.opener or 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 Exception as e: print 'Download error:', str(e) html = '' if hasattr(e, 'code'): code = e.code if num_retries > 0 and 500 <= code < 600: # retry 5XX HTTP errors return self._get(url, headers, proxy, num_retries - 1, data) else: code = None return {'html': html, 'code': code} class ScrapeCallback: def __init__(self): self.writer = csv.writer(open('countries.csv', 'w')) self.fields = ('area', 'population', 'iso', 'country', 'capital', 'continent', 'tld', 'currency_code', 'currency_name', 'phone', 'postal_code_format', 'postal_code_regex', 'languages') self.writer.writerow(self.fields) def __call__(self, url, html): if re.search('view', url): tree = lxml.html.fromstring(html) row = [] for field in self.fields: row.append(tree.cssselect('table > tr#places_{}__row > td.w2p_fw'.format(field))[0].text_content()) self.writer.writerow(row) 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): """Delay if have accessed this domain recently """ domain = urlparse.urlsplit(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() if __name__ == '__main__': link_crawler('http://example.webscraping.com', '/(index|view)', scrape_callback=ScrapeCallback())