天天看點

網絡爬蟲-Python

    周末沒事自己寫了個網絡爬蟲,先介紹一下它的功能,這是個小程式,主要用來抓取網頁上的文章,部落格等,首先找到你要抓取的文章,比如韓寒的新浪部落格,進入他的文章目錄,記下目錄的連接配接比如 http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html,裡面每篇文章都有個連接配接,我們現在需要做的就是根據每個連結進入并把文章複制到你自己的電腦檔案裡。這就把文章爬下來了 哈哈,不說了 直接來代碼吧

import urllib

import time

url=['']*50

j = 0

con = urllib.urlopen('http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html').read() #目錄連結

i=0

title = con.find(r'<a title=') #找到第一次出現<a title=的位置

href = con.find(r'href=',title) #找到<a title=之後出現href=的位置

html = con.find(r'.html',href)  #同上

while title != -1 and href != -1 and html != -1 and  i<50: #目錄下面大概50篇文章

    url[i] = con[href + 6:html +5] #抓取每篇文章的連結

    print  url[i]

    title = con.find(r'<a title=',html) #循環抓取每篇文章

    href = con.find(r'href=',title)

    html = con.find(r'.html',href)

    i= i+1

while j < 50:

    content = urllib.urlopen(url[j]).read() #讀取每個連結内的内容

    #print content

    filename  = url[j][-26:]

    open(filename,'w+').write(content) #把内容寫到你自己定義的檔案下

    print 'downloading' ,url[j]

    j = j+1

    time.sleep(1) #睡眠時間