Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to grab Bing search background Image by Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Python how to grab Bing search background images". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "Python how to grab Bing search background images" together!

First, we install IDE, here I choose Python's most popular PyCharm, you can download it on the official website:

https://www.jetbrains.com/pycharm/download/#section=windows

The installation method is very simple, just go to the next step.

After installation, open the IDE and we create a Python project

After completion, we also need to install several libraries in advance, so that we can write code later, namely:

request

BeautifulSoup4

lxml

The installation method is very simple, we click File->Settings in the upper left corner of the compiler pop-up dialog box:

We double-click pip in the picture above, search for the three library names listed above in the pop-up dialog box, and then click InstallPackage in the lower left corner to complete the installation:

After we finish, we start writing code:

First we introduce the four package codes we need:

if __name__=='__main__': for i in range(8): url = 'https://cn.bing.com/HPImageArchive.aspx? idx={}&n=1'.format(i) html = get_page(url) soup = BeautifulSoup(html, 'lxml') text = soup.find(name='url').string img_url = 'https://cn.bing.com' + text img_name = re.match('^/th\? id=(.*?)& ', text).group(1) download(img_url, 'fill in the path of the file you want to save c:/.. ', img_name)

Then we define a get_page function to get the content of the page requested by the request, but to disguise it as a browser visit, we need to change the User-Agent field here:

def get_page(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36' } response = requests.get(url, headers=headers) if response.status_code == 200: The response status code indicates the result of the server's response to the request. 200 means server response successful, 403 means access prohibited, 404 means page not found return response.text

Then define a download function for downloading images. The parameters passed include the url path of the image, the folder path you define yourself, and the name of the image:

def download(url, path, fname): response = requests.get(url) if response: with open(os.path.join(path, fname), 'wb') as f: f.write(response.content) print('successful: {} . '.format(fname)) else: print('faild: {}. '.format(fname))

Well, after the above two main functions are defined, we will define the main function to call them constantly. Note that the path of the download function should fill in your own folder path. Since Bing officially only saved eight original images, we simply and roughly only cycle 8 times, the code is as follows:

if __name__=='__main__': for i in range(8): url = 'https://cn.bing.com/HPImageArchive.aspx? idx={}&n=1'.format(i) html = get_page(url) soup = BeautifulSoup(html, 'lxml') text = soup.find(name='url').string img_url = 'https://cn.bing.com' + text img_name = re.match('^/th\? id=(.*?)& ', text).group(1) download(img_url, 'fill in the path of the file you want to save c:/.. ', img_name)

All right, so that's the whole code up there, and we're going to try and run it once.

The result was completely fine. The pictures in the folder were also saved:

Thank you for reading, the above is "Python how to grab Bing search background image" content, after the study of this article, I believe that we have a deeper understanding of Python how to grab Bing search background image this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report