python爬虫-->获取数据

在Python爬虫基础博文中,python爬虫基础,写了一个获取深度为maxdepth内所有url函数,并且下载其网页。那么这篇博文我将详细讲解如何从这些下载的网页中获取我们想要数据。首先我们先得对python正则表达式有所了解,打开这个网页查看python正则表达式
还需要对正则表达式里面一些常见的很容易混淆方法,例如re.search,re.match,有区别性的认识,打开这个网页查看Python中re的match、search、findall、finditer区别
有三种网页抓取方法

  • 正则表达式
  • BeautifulSoup模块
  • 强大的lxml模块

1)正则表达式

import re
from new_chapter1.common import download

url="http://example.webscraping.com/places/default/view/United-Kingdom-239"

html=download(url)
##查找html内class="w2p_fw"内的文本内容,以列表形式返回。
print re.findall('(.*?)',html)

2)BeautifulSoup模块

import urllib2
from bs4 import BeautifulSoup

def scrape(html):
    ##将下载的html解析为soup文档。并对其格式进行修正
    soup = BeautifulSoup(html)
    ## 定位id=places_area__row处
    tr = soup.find(attrs={'id':'places_area__row'}) # 定位id=places_area__row
    ##定位id==places_area__row的子标签class=w2p_fw处
    td = tr.find(attrs={'class':'w2p_fw'})  # 定位class=w2p_fw
    area = td.text  # 获取class=w2p_fw内的文本内容
    return area

if __name__ == '__main__':
    html = urllib2.urlopen('http://example.webscraping.com/places/default/view/United-Kingdom-239').read()
    print scrape(html)

3)lxml模块
lxml模块有几种不同办法来获取数据,比如XPath,这里我们选用CSS选择器。CSS选择器表示选择元素所使用的模式。下面是一些常用的选择器示例。
选择所有标签: *
选择标签:a
选择所有class=’link’的元素:.link
选择class=”link”的
标签: a.link 例如
选择 id=”home”的
标签:a#home 例如
选择父元素为
标签的所有子标签:a>span
选择<
a>标签内部的所有标签:a span
选择title属性为”Home”的所有
标签:a[title=Home]

import urllib2
import lxml.html
def scrape(html):
    tree = lxml.html.fromstring(html)
    td = tree.cssselect('tr#places_neighbours__row > td.w2p_fw')[0]
    area = td.text_content()
    return area

if __name__ == '__main__':
    html = urllib2.urlopen('http://example.webscraping.com/places/default/view/United-Kingdom-239').read()
    print scrape(html)

然后咱们做个对比实验,比较这三种方法的快慢

# -*- coding: utf-8 -*-

import csv
import time
import urllib2
import re
import timeit
from bs4 import BeautifulSoup
import lxml.html

FIELDS = ('area', 'population', 'iso', 'country', 'capital', 'continent', 'tld', 'currency_code', 'currency_name', 'phone', 'postal_code_format', 'postal_code_regex', 'languages', 'neighbours')


def regex_scraper(html):
    results = {}
    for field in FIELDS:
        ## re.search(patten,string):若string中含有patten子串,则返回match对象,注意是match对象不是文本内容。如果
        ##string中存在多个patten子串,只返回第一个
        ##{} 匹配对应format(field)
        results[field] = re.search('.*?(.*?)'.format(field), html).groups()[0]
    return results


def beautiful_soup_scraper(html):
    soup = BeautifulSoup(html, 'html.parser')
    results = {}
    for field in FIELDS:
        results[field] = soup.find('table').find('tr', id='places_{}__row'.format(field)).find('td', class_='w2p_fw').text
    return results


def lxml_scraper(html):
    tree = lxml.html.fromstring(html)
    results = {}
    for field in FIELDS:
        results[field] = tree.cssselect('table > tr#places_{}__row > td.w2p_fw'.format(field))[0].text_content()
    return results


def main():
    '''
    依次用以上三种方法爬虫爬1000次,并检查爬取结果是否正确,然后打印用时。
    '''
    times = {}
    html = urllib2.urlopen('http://example.webscraping.com/places/default/view/United-Kingdom-239').read()
    NUM_ITERATIONS = 1000 # number of times to test each scraper
    for name, scraper in ('Regular expressions', regex_scraper), ('Beautiful Soup', beautiful_soup_scraper), ('Lxml', lxml_scraper):
        times[name] = []
        # record start time of scrape
        start = time.time()
        for i in range(NUM_ITERATIONS):
            if scraper == regex_scraper:
                # 正则表达式默认情况下有缓存,为了与其他爬虫对比更公平
                # 我们用re.purge()清除缓存
                re.purge()
            result = scraper(html)

            # check scraped result is as expected
            assert(result['area'] == '244,820 square kilometres')
            times[name].append(time.time() - start)
        # record end time of scrape and output the total
        end = time.time()
        print '{}: {:.2f} seconds'.format(name, end - start)

    writer = csv.writer(open('times.csv', 'w'))
    header = sorted(times.keys())
    writer.writerow(header)
    for row in zip(*[times[scraper] for scraper in header]):
        writer.writerow(row)


if __name__ == '__main__':
    main()

打印结果:
Regular expressions: 3.14 seconds
Beautiful Soup: 25.48 seconds
Lxml: 3.75 seconds
显然Beautiful Soup最慢,因为Beautiful Soup是用纯python写的速度较慢,正则表达式虽然快,但是灵活性不高,随着网页结构的变化,其适用性不强。最后是lxml模块,该模块是用c语言编写的,所以速度并不慢。

在python爬虫基础博文中,我们已经存储了深度为maxdepth内所有urls,在这里我们就下载这些url对应的html网页,并获取网页内数据。我们可以设置一个回调函数,当我们获取url时,就调用这个回调函数获取网页内数据。

回调函数

import csv
import re
import urlparse
import lxml.html
from link_crawler import link_crawler
class ScrapeCallback:
    def __init__(self):
        '''
        创建一个本地csv文件,并且加上表头列名
        '''
        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', 'neighbours')
        self.writer.writerow(self.fields)

    def __call__(self, url, html):
        if re.search('/view/', url):##当这个url里面含有/view/
            tree = lxml.html.fromstring(html)##解析这个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)

if __name__ == '__main__':
    link_crawler('http://example.webscraping.com/', '/(index|view)', scrape_callback=ScrapeCallback())

获取maxdepth深度内所有url函数,并在获取url调用回调函数获取数据

#coding:utf-8
import re
import urlparse
import urllib2
import time
from datetime import datetime
import robotparser
import Queue


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=None):
    """Crawl from the given seed URL following links matched by link_regex
    """
    # the queue of URL's that still need to be crawled
    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)
    throttle = Throttle(delay)
    headers = headers or {}
    if user_agent:
        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 [])##调用回调函数,获取其html内相关数据。

            if depth != max_depth:
                # can still crawl further
                if link_regex:
                    # link_regex:(index|view),使用get_link(html)获取html内所有网址链接,re.match
                    # 从网址开头就匹配(index|view),故这个网址链接是相对路径,所以后面还要normalize(seed_url, link)
                    # 使其变为绝对路径
                    links.extend(link for link in get_links(html) if re.search(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:
    """
     爬虫速度过快,可能会造成服务器过载,或者是ip地址被封,为了避免这个问题,我们的爬虫将会设置一个delay标识,
    用于设定请求同一域名时最小时间间隔。注意是同一域名。
    爬取同一域名下不同网页时,需要注意两次下载之间至少需要1秒钟的间隔。
    """

    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()


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
                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, _ = urlparse.urldefrag(link)  # remove hash to avoid duplicates
    return urlparse.urljoin(seed_url, link)

def same_domain(url1, url2):
    """Return True if both URL's belong to same domain
    """
    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
    webpage_regex = re.compile(']+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')

你可能感兴趣的:(python爬虫)