哎,以前寫博文的時候沒注意,有些圖片用QQ來截取,獲得的圖片文件名都是類似于QQ截圖20120926174732-300×15.png的形式,昨天用ftp備份網站文件的時候發現,中文名在flashfxp里面顯示的是亂碼的,看起來好難受,所以寫了一個python小腳本,爬取整個網站,然后獲取每個文章頁面的圖片名,并判斷如果是類似于QQ截圖20120926174732-300×15.png的形式就輸出并將該圖片地址和對應的文章地址保存在文件中,然后通過該文件來逐個修改。
好了,下面是程序代碼:
import urllib2from bs4 import BeautifulSoupimport reimport sysreload(sys)sys.setdefaultencoding('utf-8') baseurl = "http://www.jb51.net/dont-worry.html"#說明下,起始地址是第一篇文章的地址,通過該文章的頁面就#可以使用BeautifulSoup模塊來獲取上一篇文章的地址 file = open(r"E:/123.txt","a") def pageloop(url): page = urllib2.urlopen(url) soup = BeautifulSoup(page) img = soup.findAll(['img']) if img == []: print "當前頁面沒有圖片" return else: for myimg in img: link = myimg.get('src') print link pattern = re.compile(r'QQ/S*[0-9]*png') badimg = pattern.findall(str(link)) if badimg: print url file.write(link + "/n") file.write(url+"/n") def getthenextpage(url): pageloop(url) page = urllib2.urlopen(url) soup = BeautifulSoup(page) for spanclass in soup.findAll(attrs={"class" : "article-nav-prev"}): #print spanclass if spanclass.find('article-nav-prev') != -1: pattern = re.compile(r'//www.jb51.net//S*html') pageurl = pattern.findall(str(spanclass)) for i in pageurl: #print i getthenextpage(i) getthenextpage(baseurl) print "the end!"file.close()
最后,對和我以前剛開始做網站的同學說下,圖片命名的話最好是用數字形式或者是英文、拼音的形式,要不然到最后想修改的話就麻煩了,所以最好就是從剛開始就養成好的習慣,用正確的命名規范來問文章、圖片來命名,這樣就會好很多。
新聞熱點
疑難解答