How to climb the most beautiful picture in the simplest way in Python
This article introduces how to use the simplest way to climb the best-looking pictures in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Preface
Next, we have a very basic BeautifulSoup crawler library to write down the process of crawling pictures, download and store pictures, which I believe everyone can understand.
No more nonsense, just go to the code.
Import the library first
Import urllib.requestfrom bs4 import BeautifulSoupimport os
Analyze the website
Url = 'http://www.8she.com/31988.html'res = urllib.request.urlopen (url) html = res.read (). Decode (' utf-8') soup = BeautifulSoup (html, 'html.parser') result = soup.find_all (class_='aligncenter', limit=15) print (result) links = [] for content in result: links.append (content.get (' src'))
Download and save pictures
If not os.path.exists ('D:\\ rieuse\ reptile picture\ photo2'): # create os.makedirs ('D:\\ rieuse\ reptile picture\ photo2') I = 0for link in links: I + = 1 filename ='D:\\ rieuse\ reptile picture\ photo2\'+ 'photo' + str (I) +' .jpg' with open (filename,'w') as file: urllib.request.urlretrieve (link) if the path does not exist Filename) priint ('download Picture:', str (I))
Haha, is it a very simple process of crawling pictures? Welcome to communicate, study together, report mistakes together and make progress together.
About how to use the simplest way to climb the best-looking pictures in Python, so much for sharing here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.