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

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

  • 相关阅读:
    Perl-晶晨2021届笔试题
    数字IC设计流程
    后端一些常考知识点
    sklearn: 利用TruncatedSVD做文本主题分析
    用截断奇异值分解(Truncated SVD)降维
    numpy.linalg.norm(求范数)
    岭回归和lasso回归及正则化
    什么是范数?
    MySQL三大范式和反范式
    汇编知识之EIP寄存器
  • 原文地址:https://www.cnblogs.com/xueweihan/p/4590962.html
Copyright © 2011-2022 走看看