哎,以前寫博文的時候沒注意,有些圖片用QQ來截取,獲得的圖片文件名都是類似于QQ截圖20120926174732-300×15.png的形式,昨天用ftp備份網站文件的時候發現,中文名在flashfxp里面顯示的是亂碼的,看起來好難受,所以寫了一個python小腳本,爬取整個網站,然后獲取每個文章頁面的圖片名,并判斷如果是類似于QQ截圖20120926174732-300×15.png的形式就輸出并將該圖片地址和對應的文章地址保存在文件中,然后通過該文件來逐個修改。
好了,下面是程序代碼:
import urllib2
from bs4 import BeautifulSoup
import re
import sys
reload(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()
最后,對和我以前剛開始做網站的同學說下,圖片命名的話最好是用數字形式或者是英文、拼音的形式,要不然到最后想修改的話就麻煩了,所以最好就是從剛開始就養成好的習慣,用正確的命名規范來問文章、圖片來命名,這樣就會好很多。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

