search
HomeBackend DevelopmentPython Tutorialpython采集博客中上传的QQ截图文件

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

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

Statement
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
What is Python Switch Statement?What is Python Switch Statement?Apr 30, 2025 pm 02:08 PM

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

What are Exception Groups in Python?What are Exception Groups in Python?Apr 30, 2025 pm 02:07 PM

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

What are Function Annotations in Python?What are Function Annotations in Python?Apr 30, 2025 pm 02:06 PM

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

What are unit tests in Python?What are unit tests in Python?Apr 30, 2025 pm 02:05 PM

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

What are Access Specifiers in Python?What are Access Specifiers in Python?Apr 30, 2025 pm 02:03 PM

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

What is __init__() in Python and how does self play a role in it?What is __init__() in Python and how does self play a role in it?Apr 30, 2025 pm 02:02 PM

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

What is the difference between @classmethod, @staticmethod and instance methods in Python?What is the difference between @classmethod, @staticmethod and instance methods in Python?Apr 30, 2025 pm 02:01 PM

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

How do you append elements to a Python array?How do you append elements to a Python array?Apr 30, 2025 am 12:19 AM

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools