python3 写网络爬虫实战一

import re
import urllib.parse
import urllib.request
import time
from datetime import datetime
import urllib.robotparser
import queue
import csv
import lxml.html

class ScrapeCallback:
    def __init__(self):
        # 以‘w’方式打开csv文件,默认以\n结尾,newline=''是为了防止产生空行,用‘’代替\n
        self.writer = csv.writer(open('countries.csv', 'w', newline=''))
        self.fields = ('area', 'population', 'iso', 'country', 'capital', 'continent', 'tid', 'currency_code',
                       'currency_name', 'phone', 'postal_code_format', 'postal_code_regex', 'language', 'neighbours')
        # 将fields写进csv文件中,所有数据都默认写在一行中
        self.writer.writerow(self.fields)

    def __call__(self, url, html):
        # 使用re.search匹配url中的‘/view/’,若匹配得到,则是国家页面,可把数据写进csv文件中(index页面则没有)
        if re.search('/view/', url):
            html = html.decode('utf-8')
            # 返回的是lxml.html.HtmlElement
            tree = lxml.html.fromstring(html)
            row = []
            for field in self.fields:
                # 使用css选择器,可查看其相关用法,将field的值映射到字符串中的{}中,cssselect返回一个list
                if len(tree.cssselect('table > tr#places_{}__row > td.w2p_fw'.format(field))) > 0:
                    row.append(tree.cssselect('table > tr#places_{}__row > td.w2p_fw'.format(field))[0].text_content())
            self.writer.writerow(row)


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, scrape_callback=ScrapeCallback()):
    """Crawl from the given seed URL following links matched by link_regex
    """
    # crawl_queue列表记录未访问的链接
    crawl_queue = [seed_url]
    # seed字典记录所有链接及其深度
    seen = {seed_url: 0}
    # 跟踪记录下载过的url的数量
    num_urls = 0
    # 获取主链接的robots.txt,用于后面判断是否可用某一代理进行访问
    rp = get_robots(seed_url)
    # 初始化限速类
    throttle = Throttle(delay)

    headers = headers or {}
    if user_agent:
        # user_agent可以理解为用户访问时的标识
        # 设置user_agent可以理解为伪装浏览器对网站进行访问,可以对浏览器的UA字串的标准格式进一步了解
        headers['User-agent'] = user_agent

    while crawl_queue:
        url = crawl_queue.pop()
        depth = seen[url]
        # 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 = []
            # 若有回调函数,则先执行
            if scrape_callback:
                links.extend(scrape_callback(url, html) or [])

            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
                        # 检查是否为同一个域名下的链接
                        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):
        """Delay if have accessed this domain recently
        """
        # 获取链接的域名
        domain = urllib.parse.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 = urllib.request.Request(url, data, headers)
    opener = urllib.request.build_opener()
    # proxy主要是伪装ip地址进行访问,防止同一个地址访问得过于频繁,注意与user_agent进行区分
    if proxy:
        proxy_params = {urllib.parse.urlparse(url).scheme: proxy}
        opener.add_handler(urllib.request.ProxyHandler(proxy_params))
    try:
        response = opener.open(request)
        html = response.read()
        code = response.code
    except urllib.request.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
                html = 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, _ = urllib.parse.urldefrag(link)  # remove hash to avoid duplicates
    return urllib.parse.urljoin(seed_url, link)


def same_domain(url1, url2):
    """Return True if both URL's belong to same domain
    """
    return urllib.parse.urlparse(url1).netloc == urllib.parse.urlparse(url2).netloc


def get_robots(url):
    """Initialize robots parser for this domain
    """
    rp = urllib.robotparser.RobotFileParser()
    rp.set_url(urllib.parse.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
    webpage_regex = re.compile('<a[^>]+href=["\'](.*?)["\']', re.IGNORECASE)
    # list of all links from the webpage
    html = html.decode('utf-8')
    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')
    link_crawler('http://example.webscraping.com', '/places/default/(index|view)', scrape_callback=ScrapeCallback())

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值