Home > Backend Development > Python Tutorial > python采集博客中上传的QQ截图文件

python采集博客中上传的QQ截图文件

WBOY
Release: 2016-06-16 08:43:10
Original
1489 people have browsed it

哎,以前写博文的时候没注意,有些图片用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'http://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()
Copy after login

最后,对和我以前刚开始做网站的同学说下,图片命名的话最好是用数字形式或者是英文、拼音的形式,要不然到最后想修改的话就麻烦了,所以最好就是从刚开始就养成好的习惯,用正确的命名规范来问文章、图片来命名,这样就会好很多。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template