How to climb Bing wallpaper by Python
This article introduces the relevant knowledge of "how to climb Bing wallpaper by Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
A person who likes to toss around, seeing that there is only one kind of wallpaper on the table for a long time, wants to crawl some wallpaper from the Internet, save it, and switch wallpaper at random.
Tools
Python3 + requests + re
Fiddler bag grabbing tool.
Then you can be sure that the url of the picture must be in the web source code.
We can see the picture we want url in the source code.
It can be grasped by regularization.
Import requestsimport threadingimport jsonimport refrom pprintimport pprintimport timeclass Bing: def _ init__ (self): self.url = "https://bing.ioliu.cn/?p={}" self.headers = {" User-Agent ":" Mozilla/5.0 (Windows NT 10.0; Win64) X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 "} def parse_url (self, url): print (" * "* 20) response = requests.get (url, headers=self.headers) return response.content def regular_img_name (self) Img_url): ret = re.findall (r "http://h2.ioliu.cn/bing/(.*?)\.(.*?)$", img_url, re.M | re.S) print (ret) return ret def download_imgs (self) Img_list): for url in img_list: img = self.parse_url (url) time.sleep (1) strs = self.regular_img_name (url) with open (". / bing/ {}. {}" .format (strs [0] [0], strs [0] [1]) "wb") as f: f.write (img) print ("{} success" .format (url)) def regular_img_url (self, html_str): ret = re.findall (r ". *? pic= (. *?)\? imageslim.*?", html_str, re.M | re.S) return ret def run (self): for i in range (1 10): url = self.url.format (I) html_str = self.parse_url (url). Decode () img_list = self.regular_img_url (html_str) print (img_list) self.download_imgs (img_list) if _ name__ = "_ _ main__": bing = Bing () bing.run () That's all for "how Python crawls Bing wallpaper". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!