Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use python to climb a girl's picture

Shulou Source: shulou.com Published: 2022-06-02 03:13:39 09月12日 Update

This article is about how to use python to climb girls' pictures, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I used to talk about the use of python crawler library, but I haven't said how to make good use of this knowledge to play with something fun. Then I'll take you to play fun and exciting today, hee hee! By the way, requests libraries and regular expressions are very important, be sure to learn! You must learn! You must learn! Most of my current reptiles use these two things to climb. So it's up to you whether you learn from it or not.

To today's focus, I found a website very easy to climb, very suitable for beginners. I climbed many times without setting a request header or anything, and did not seal my ip and return 403 to me, so he was very friendly to us crawlers for the first time. This website is Jinri Toutiao. The most important thing is that there are a lot of beautiful pictures here, we can climb them down! Do you have a nosebleed when you think about it?

What we are going to climb today is his atlas. Take a look at the website first. Search for beautiful women, and then click on the atlas to see the following

All we have to do is climb down the picture above.

Then start analyzing the website. Press F12, then click network, refresh and you can see these.

To find out which request to return these pictures, you can see on the web page that the pictures will display more pictures as you drop down the web page, which is dynamically loaded, so it is easy to know that this can be found in the xhr file, sure enough, you see

Keep pulling down, keep sending requests, and click on this request to see what data is returned

You can see that this is a json, there is a picture of the url, this is what we are looking for, then we can use the json library to parse, and this site is a get request, so you can use the requests library to send and parse it, very simple.

Then the analysis ends here and goes directly to the code.

Import requests, os

Path_a = os.path.abspath ('.')

Kw =''

While True:

Kw = input ('Please enter the picture you want to get (enter 1 if you want to finish)')

If kw = = '1mm:

Print ('exited, the picture you downloaded has been saved in' + path_a+', please check!')

Break

For x in range (0, 1000, 20):

Url = 'https://www.toutiao.com/search_content/?offset='+str(x)+'&format=json&keyword=%s&autoload=true&count=20&cur_tab=3&from=gallery'% kw

Response = requests.get (url)

Data = response.json () ['data']

If not data:

Print ('download' + kw+' picture is finished, please change the keyword to continue)

Break

N = 1 # number of articles recorded

For atlas in data:

# create a directory

Title = atlas ['title']

Print (atlas)

Try:

If title not in os.listdir ('.'): # prevent the file name from already existing

Os.mkdir (title)

Except OSError as e:

Print ('file name error, failed to create directory, recreate a random name')

Title = kw + 'File name error' + str (x)

If title not in os.listdir ('.')

Os.mkdir (title)

K = 1 # record the number of pictures downloaded

Path = os.path.join (path_a, title)

# go to the picture directory

Os.chdir (path)

For image in atlas ['image_list']: # the picture obtained by this link is small, and it doesn't look good enough, so replace it with a big picture below.

Image_url = image ['url']. Replace (' list', 'large') # change a link to get a big picture

Atlas = requests.get ('http:'+image_url). Content

With open (str (k) + '.jpg', 'wb') as f: # write the picture to the file

F.write (atlas)

Print ('% d images complete after downloading% d article'% (xann, k))

K + = 1

N + = 1

# transfer out the picture directory

Os.chdir (path_a)

This can be done with only the requests library, which is used to manipulate the file directory, so I won't go into details here. As you can see, the amount of code is very small, except for comments, it is about 40 lines, is it much more concise than other languages? Is the requests library easy to use? Here can fully reflect the fact that life is short, I use the truth of python.

Moreover, he can also change the keyword to continue to search, you can search for any photos you want.

Finally, I'll show you the results.

Needless to say, I'm going to buy Nutrition Express.

The above is how to use python to climb girls' pictures. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Pictures files that is directories websites things file names articles more crawlers knowledge girls fun important continuous code key atlas articles web pages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple NVidia Redmi Microsoft