Male students in the process of learning the automation of Python is very interested in reptiles, some students want to be able to collect some erotic pictures for their own appreciation. As instructors can only be satisfied with the desire to help everyone achieve the pursuit of beauty, http://wanimal.lofter.com/?page=1, ~_~
First step: Analyze the Request page
We teach you in class in Firefox, to analyze the HTML source code of the request page, we request the page to find the Law page is http://wanimal.lofter.com/?page=2, as long as the number 2 replaced, the number can be turned to page, Then the image address of each page is found in the following rules
<span style= "Font-family:microsoft yahei;font-size:18px;" ><div class= "Imgwrapper" >
<a href= "HTTP://WANIMAL.LOFTER.COM/POST/17D0D7_2D2BEB5" >
</a>
</div></span>
Step Two: Implement
Import requests# first importrequests and Shutils
Import Shutil
From BS4 import beautifulsoup# re-import we just said Bs.
def download (page): #这个函数用来寻找, download, save picture
url = "http://wanimal.lofter.com?page=%d"% page# here is the URL to write the destination address and easy to loop around the page (all pages are downloaded)
html = requests.get (URL). content# here is a requests to get the source code of the entire page and organize
photos = beautifulsoup (HTML). Find_all (class_= "Imgwrapper") #用BeautifulSoup解析html, then use the function of BS, Find all items of class imgwrapper exist in a list
For photo in photos:
img_url = photo.img["src"] #这里是获得上面每一条里面的img (with this method) the string stored in "src" (that is, the real address of the picture)
# Print photo, Img_url
img_filename = "img/" + img_url.split ("/") [-1] #使用切片工具切出每一个文件名
img = requests.get (img_url, stream=true) #保存了
if Requests.get (img_url). Status_code = =:
with open (Img_filename, ' WB ') as F:
Shutil.copyfileobj (Img.raw, F)
print Img_filename, "saved." #完成了叫一声
if __name__ = = ' __main__ ':
For page in Xrange (1,33):
Download (page) </span>
Test Development Python Training: Achieve the yellow picture collection wish of Cock Silk (episode)