標籤:
#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