[Python]爬蟲v0.1

來源:互聯網
上載者:User

標籤:

#coding:utf-8import urllib#######爬蟲v0.1 利用urlib2 和 字串內建函數####### 擷取網頁內容def getHtml(url):    page = urllib.urlopen(url)    html = page.read()    return htmldef 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

只是粗略的記錄寫爬蟲的過程和思路,本來打算直播的,但是我們十一點斷電斷網。明天續寫這個文章,直播寫爬蟲。哈哈哈,雖然基礎,但是也是寫出來吧。

 

[Python]爬蟲v0.1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.