zoukankan      html  css  js  c++  java
  • [Python]爬虫v0.1

    #coding:utf-8
    import urllib
    ######
    #爬虫v0.1 利用urlib2 和 字符串内建函数
    ######
    
    # 获取网页内容
    def getHtml(url):
        page = urllib.urlopen(url)
        html = page.read()
        return html
    
    def content(html):
        # 内容分割的标签
        str = '<article class="article-content">'
        content = html.partition(str)[2]
        str1 = '<div class="article-social">'
        content = content.partition(str1)[0]
        return content # 得到网页的内容
        
    def title(content,beg = 0):
        # 思路是利用str.index()和序列的切片
        try:
            title_list = []
            while beg >=0:   
                num1 = content.index('',beg)
                num2 = content.index('</p>',num1)
                title_list.append(content[num1:num2])
                beg = num2
            
        except ValueError:
             return title_list
            
            
        
    def get_title():
        # 利用循环更新num1和num2,从而匹配出全部title
        pass
        
    content = content(getHtml("http://bohaishibei.com/post/10449/"))
    #num = content.index('】')
    title = title(content)
    for i,e in enumerate(title):
        print '第%d个,title:%s' % (i,e)
    
    # 今天爬的单个页面的title

    只是粗略的记录写爬虫的过程和思路,本来打算直播的,但是我们十一点断电断网。明天续写这个文章,直播写爬虫。哈哈哈,虽然基础,但是也是写出来吧。

  • 相关阅读:
    AttributeError: '_csv.reader' object has no attribute 'next'
    AttributeError: type object '_io.StringIO' has no attribute 'StringIO'
    sklearn学习笔记2
    sklearn学习笔记1
    隐语义模型LFM(latent factor model)
    windows下python3.4安装scikit-learn
    关联规则1
    关联规则
    Jmeter上传文件
    jmeter 学习笔记
  • 原文地址:https://www.cnblogs.com/xueweihan/p/4590962.html
Copyright © 2011-2022 走看看