Python collects QQ files uploaded from blogs,
And save the image address and the corresponding article address in the file, and then modify them one by one through the file.
Well, the following is the program code:
Import urllib2from bs4 import BeautifulSoupimport reimport sysreload (sys) sys. setdefaultencoding ('utf-8') baseurl = "http://www.bkjia.com/dont-worry.html" # note that the starting address is the address of the first article, through the page of this article # You can use the BeautifulSoup module to obtain the address file = open (r "E: \ 123.txt"," a ") def pageloop (url) of the previous article ): page = urllib2.urlopen (url) soup = BeautifulSoup (page) img = soup. findAll (['img ']) if img = []: print "no image on the current page" return else: for myimg I N 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.bkjia.com/?s=html') pageurl = pattern. findall (str (spanclass) for I in pageurl: # print I getthenextpage (I) getthenextpage (baseurl) print "the end! "File. close ()
Finally, let me talk to my colleagues who have just started website preparation. It is best to name images in the form of numbers, English letters, and pinyin. Otherwise, it will be difficult to modify the image, therefore, it is better to develop a good habit from the very beginning and use the correct naming rules to name articles and images.