Python爬蟲(二)

來源:互聯網
上載者:User

標籤:spider   awl   open   pen   __init__   nbsp   title   begin   font   

爬取電影吧一個文章裡的所有樓主發言:

# python2# -*- coding: utf-8 -*-import urllib2import stringimport reclass Baidu_Spider:    feature_pattern = re.compile(r‘id="post_content.*?>\s+(.*?)</div>‘, re.S)    replaceList = [(‘&#39;‘, ‘\‘‘), (‘&quot;‘, ‘\"‘)]    def __init__(self, url):        self.url = url + ‘?see_lz=1‘    def crawl_tieba_lz(self):        begin_page = urllib2.urlopen(self.url).read()        self.print_page_title(begin_page)        count = self.get_page_count(begin_page)        self.handle_data(count)    def handle_data(self, count):        f = open(‘tieba_lz.txt‘, ‘w+‘)        for i in range(count):            url = self.url + ‘&pn=‘ + str(i+1)            hint = ‘第‘ + str(i+1) + ‘頁‘            print ‘正在下載%s: %s‘ % (hint, url)            page = urllib2.urlopen(url).read()            features = re.findall(self.feature_pattern, page)            print hint + ‘下載完成‘            print ‘共有%d條記錄‘ % len(features)            f.write(hint + ‘:\n‘)            for feature in features:                feature = self.handle_record(feature)                print feature                f.write(feature + ‘\n\n‘)        f.close()        print ‘done‘    def handle_record(self, record):        record = re.sub(r‘(<|</)br>‘, ‘\n‘, record)        record = re.sub(r‘<.*?>‘, ‘‘, record)        for item in self.replaceList:            record = record.replace(item[0], item[1])        return record    def get_page_count(self, page):        result = re.search(r‘class="red">(\d+?)</span>‘, page, re.S)        if result:            count = int(result.group(1))            print ‘一共%d頁‘ % count        else:            count = 0;            print ‘無法擷取頁數‘        return count    def print_page_title(self, page):        result = re.search(r‘<h1.*?>(.*?)</h1>‘, page, re.S)        if result:            title = result.group(1)            print ‘標題: %s‘ % title        else:            print ‘無法擷取標題‘spider = Baidu_Spider(‘http://tieba.baidu.com/p/4082863285‘)spider.crawl_tieba_lz()

 

Python爬蟲(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.