current position:Home>Python crawls a large number of beautiful pictures with 10 lines of code

Python crawls a large number of beautiful pictures with 10 lines of code

2022-02-01 12:42:51 ZackSock

This is my participation 11 The fourth of the yuegengwen challenge 12 God , Check out the activity details :2021 One last more challenge

Preface

Speaking of beauty , The first thing I think of is the blind date website with a large number of beautiful women . So today, I also choose a blind date website as the material , Take pictures of beautiful women .

1、 preparation

First, you need an account of a dating website , What I choose here is “ My Lord, good luck ”. Just register and log in : The interface after login After logging in, the interface is roughly as above , Fill in some screening criteria , Then click search fate , It's the result we want . But what we have to do is crawl through the beautiful pictures , Let's right-click -> Check ->Network, Then we click to search for fate , Found more of the following :  Click to search fate, and there is an additional request content Let's order , Check it out. header What's in it : Click in and find one API We don't need to see anything else , Let's take a direct look at this website . It's just one. api, Ha ha, this is the beauty picture we want api 了 .API as follows :www.7799520.com/api/user/pc…

2、API analysis

We can URL Analyze this API Parameters of , The main parameters are as follows :

Parameters Parameter type
startage int
endage int
gender int
startheight int
endheight int
salary int
page int
You can try out which of these parameters are necessary and which are not , You can also try to limit the parameter value by yourself . Because bloggers are younger , So today's test is 21-30 year 、 height 151-160 Women of . You can modify this according to your hobbies .
## 3、Json Data analysis
After the test , Find the above API The data returned is Json data , The returned data is as follows :
{    "data": {        "list": [            {                "avatar": "http://img.7799520.com/2019-11-27-1574867191-MXUdY0Fc.png",                "birthdayyear": "1994",                "city": " Shanghai ",                "education": " Junior high school ",                "gender": "2",                "height": "159",                "marry": " unmarried ",                "monolog": " Wish to have one's heart , White heads are inseparable ",                "monologflag": "1",                "province": " Shanghai ",                "salary": "5 thousand -1 ten thousand ",                "userid": "3018330",                "username": " Single smile Shanlan "            },            {                "avatar": "http://img.7799520.com/FhTV65n3mQ-X-PjfR3W9OpsFs5SO",                "birthdayyear": "1991",                "city": " Beijing ",                "education": " Undergraduate ",                "gender": "2",                "height": "160",                "marry": " unmarried ",                "monolog": " A native Beijinger ,91 Year end Scorpio ~lxt1103 Program the ape , High pay , No room, car, small deposit ~ Fat is not tall , alas :-( Love to travel , food , Travel and eat delicious food ~ I want to find a little brother who likes sports to lose weight with me , Or boys who don't mind fat girls ~ Boys are best from Beijing , There are so many common topics , You can't be too far from Beijing , It's also very painful to catch the Spring Festival transportation. I hope you are a funny or clever blue child paper , Be happy together and have a good chat ",                "monologflag": "-1",                "province": " Beijing ",                "salary": "2 ten thousand -5 ten thousand ",                "userid": "3018171",                "username": " Tongtong Tongtong "            },            {                "avatar": "http://img.7799520.com/00d0ba6e-5807-44fd-88af-eb379b325835",                "birthdayyear": "1991",                "city": " Shenzhen ",                "education": " high school ",                "gender": "2",                "height": "155",                "marry": " unmarried ",                "monolog": " If you are sincere, you can add wechat you02457 My age requirements 30--35",                "monologflag": "-1",                "province": " guangdong ",                "salary": "1 ten thousand -2 ten thousand ",                "userid": "3017206",                "username": "( honest )"            },            {                "avatar": "http://img.7799520.com/2019-11-27-1574817016-6JBhbUyU.png",                "birthdayyear": "1989",                "city": " Xi'an ",                "education": " junior college ",                "gender": "2",                "height": "160",                "marry": " unmarried ",                "monolog": " Never too late to marry love ",                "monologflag": "-2",                "province": " shaanxi ",                "salary": "2 thousand -5 thousand ",                "userid": "3015509",                "username": "Best Yuan "            },            {                "avatar": "http://img.7799520.com/0e1ed4fa3b5ca22ed120bf08a452506b53c0da49-2019-11-27-15748275951574827595051-hSw85JrS.png",                "birthdayyear": "1995",                "city": " Shanghai ",                "education": " master ",                "gender": "2",                "height": "155",                "marry": " unmarried ",                "monolog": " I really don't know how to write this ...... Parents urge marriage every day ..... Is it a monologue ...",                "monologflag": "1",                "province": " Shanghai ",                "salary": "2 ten thousand -5 ten thousand ",                "userid": "3014896",                "username": "...123"            },            {                "avatar": "http://img.7799520.com/f9e573e4-728a-4a05-8abd-9688c6d1c156",                "birthdayyear": "1997",                "city": " ningbo ",                "education": " Junior high school ",                "gender": "2",                "height": "160",                "marry": " unmarried ",                "monolog": " Wish to have one's heart , White heads don't separate ,15058276626",                "monologflag": "-1",                "province": " Zhejiang ",                "salary": "2 thousand -5 thousand ",                "userid": "3014476",                "username": " Season is delicate "            },            {                "avatar": "http://img.7799520.com/8c328b6a-f34a-4d91-a869-10f6e47627e9",                "birthdayyear": "1992",                "city": " Shenzhen ",                "education": " Junior high school ",                "gender": "2",                "height": "158",                "marry": " unmarried ",                "monolog": " Wish to have one's heart , White head doesn't separate my micro signal chen123456qing",                "monologflag": "-1",                "province": " guangdong ",                "salary": "5 thousand -1 ten thousand ",                "userid": "3013067",                "username": " Look back on the stereo and work hard "            },            {                "avatar": "http://img.7799520.com/9f74fb99444547a1408575c346008f22ac4bb1f7-2019-11-25-15746785901574678589876-kHZrSfnc.png",                "birthdayyear": "1992",                "city": " jinan ",                "education": " junior college ",                "gender": "2",                "height": "160",                "marry": " unmarried ",                "monolog": " Maybe I'm ordinary , But I have no lack of passion for life and dream of life , Maybe I'll be alone , But I'll find you all the way . I meet you , It will be the most beautiful moment of my life .",                "monologflag": "1",                "province": " Shandong ",                "salary": "5 thousand -1 ten thousand ",                "userid": "3009076",                "username": " Proud cat king "            },            {                "avatar": "http://img.7799520.com/7da0c781-3115-467f-9fcc-d46d2aa1bb4a",                "birthdayyear": "1994",                "city": " Abroad ",                "education": " high school ",                "gender": "2",                "height": "155",                "marry": " unmarried ",                "monolog": " I have a pot of wine , Enough to comfort the wind and dust ",                "monologflag": "1",                "province": " Abroad ",                "salary": "2 thousand -5 thousand ",                "userid": "3007139",                "username": " Ink dye ."            },            {                "avatar": "http://img.7799520.com/2019-11-24-1574575893-JYE0Y9nz.png",                "birthdayyear": "1994",                "city": " The north sea ",                "education": " junior college ",                "gender": "2",                "height": "157",                "marry": " unmarried ",                "monolog": " Wish to have one's heart , White heads are inseparable , Non members , So a lot of information can't be seen , I'm sorry ",                "monologflag": "1",                "province": " guangxi ",                "salary": "5 thousand -1 ten thousand ",                "userid": "3006914",                "username": " Creeper whale "            },            {                "avatar": "http://img.7799520.com/2019-11-24-1574565615-2p6Q37YC.png",                "birthdayyear": "1995",                "city": " Guangzhou ",                "education": " Undergraduate ",                "gender": "2",                "height": "160",                "marry": " unmarried ",                "monolog": " If we're together because it's right , That hope is suitable for a lifetime .",                "monologflag": "1",                "province": " guangdong ",                "salary": "5 thousand -1 ten thousand ",                "userid": "3006237",                "username": " Giraffe to light "            },            {                "avatar": "http://img.7799520.com/4c69af45f1f9763bc33b7322cd025c90157a93b9-2019-11-23-15745152791574515278714-5F2a7dhi.png",                "birthdayyear": "1997",                "city": " Shanghai ",                "education": " junior college ",                "gender": "2",                "height": "158",                "marry": " unmarried ",                "monolog": " Pretty leather bags are the same , One in a million interesting souls ...",                "monologflag": "1",                "province": " Shanghai ",                "salary": "1 ten thousand -2 ten thousand ",                "userid": "3004596",                "username": "solely"            },            {                "avatar": "http://img.7799520.com/aaf297dd-af30-48de-8027-5c7e57ec2cdc",                "birthdayyear": "1993",                "city": " Shenzhen ",                "education": " high school ",                "gender": "2",                "height": "155",                "marry": " unmarried ",                "monolog": " In today's fast-paced society , After busy work , I hope to be accompanied by a bosom friend , Go shopping occasionally , Watch movies and eat , Travel together , motion , Share each other's joys and sorrows , Hope to appreciate each other , inclusive , understand . I think the best love is to be the best self for each other , Become the most tacit partner , Discover the beauty of the world together .",                "monologflag": "1",                "province": " guangdong ",                "salary": "5 thousand -1 ten thousand ",                "userid": "3003499",                "username": " A wood "            },            {                "avatar": "http://img.7799520.com/2019-11-22-1574436265-oOHCA0Pi.png",                "birthdayyear": "1991",                "city": " Shanghai ",                "education": " high school ",                "gender": "2",                "height": "153",                "marry": " unmarried ",                "monolog": " Dancing girls who love watermelon ?",                "monologflag": "-2",                "province": " Shanghai ",                "salary": "5 thousand -1 ten thousand ",                "userid": "3001594",                "username": " Watermelon melon "            },            {                "avatar": "http://img.7799520.com/6351f7c2-734d-484f-95ae-7881b3b65132",                "birthdayyear": "1996",                "city": " nanchang ",                "education": " secondary specialized school ",                "gender": "2",                "height": "158",                "marry": " unmarried ",                "monolog": " There is a response to everything , Gradually there is a landing ",                "monologflag": "1",                "province": " jiangxi ",                "salary": "2 thousand -5 thousand ",                "userid": "2999190",                "username": "977"            },            {                "avatar": "http://img.7799520.com/bc692905b97d0deeb6df0f73356d3de82b1d6261-2019-11-23-15745076641574507664470-zV4AFL8O.png",                "birthdayyear": "1990",                "city": " Chengdu ",                "education": " junior college ",                "gender": "2",                "height": "156",                "marry": " unmarried ",                "monolog": " Northeast people in Chengdu ! The picture was taken many years ago . I don't like taking photos, so I don't have any photos now ! My height 155 weight 42 kg . Don't like : A boy of sheep , You'd better not smoke or drink ! I'm a snake Scorpio ️",                "monologflag": "1",                "province": " sichuan ",                "salary": "2 thousand -5 thousand ",                "userid": "2998289",                "username": " The kettle is in love and speechless "            },            {                "avatar": "http://img.7799520.com/7384954e-5c0d-4a5c-92c6-4493ba1be3d4",                "birthdayyear": "1995",                "city": " Suzhou ",                "education": " junior college ",                "gender": "2",                "height": "160",                "marry": " unmarried ",                "monolog": " hi   Hello   Can you bring me a super big cup of happiness ",                "monologflag": "1",                "province": " jiangsu ",                "salary": "2 thousand -5 thousand ",                "userid": "2991868",                "username": " Small, so small, quiet "            },            {                "avatar": "http://img.7799520.com/FlUJTeR0REKbLhtoR5RNVeuOXRy1",                "birthdayyear": "1992",                "city": " Suzhou ",                "education": " junior college ",                "gender": "2",                "height": "160",                "marry": " unmarried ",                "monolog": " Love good-looking animation and novels , Comparative house , Like to have a plan , Like to be alone , At ease . The ideal partner is to have a stable job ....",                "monologflag": "1",                "province": " jiangsu ",                "salary": "2 thousand -5 thousand ",                "userid": "2989769",                "username": " Qingyimu "            },            {                "avatar": "http://img.7799520.com/edbb6516-2b07-401e-b56e-6aee6c2620ca",                "birthdayyear": "1994",                "city": " Bazhong ",                "education": " high school ",                "gender": "2",                "height": "156",                "marry": " unmarried ",                "monolog": " I'm looking for someone , I feel ok , You can add JC718829",                "monologflag": "-1",                "province": " sichuan ",                "salary": "5 thousand -1 ten thousand ",                "userid": "2989629",                "username": " The star wishes to look back on the sadness "            },            {                "avatar": "http://img.7799520.com/e648d317faacffb4f03b1ca31fdbed2b4c6ec5e4-2019-11-25-15746776401574677640473-C6Z1QX0K.png",                "birthdayyear": "1990",                "city": " Shenzhen ",                "education": " Junior high school ",                "gender": "2",                "height": "159",                "marry": " unmarried ",                "monolog": " Wish to have one's heart , White heads are inseparable ",                "monologflag": "1",                "province": " guangdong ",                "salary": "2 thousand -5 thousand ",                "userid": "2988102",                "username": " Ramashanti "            }        ],        "num": 20,        "page": 1    },    "error_code": 0}
 Copy code 

json The general structure of the data We can analyze this structure to get the information we need .

4、 Code explanation

If you have used reptiles, you will generally feel Python The reptile is very simple , As the title says , It only needs 10 Line code , The code is as follows :

import requests		# Import request package 
dir = 'C:/Users/zaxwz/Desktop/xqImg/'	# The folder path used to store pictures 
# The image url, I'm here page No parameters were given , For the convenience of changing pages later 
url = 'http://www.7799520.com/api/user/pc/list/search?startage=21&endage=30&gender=2&startheight=151&endheight=160&marry=1&salary=3&page='
# Use the cycle , Crawling 40 The beauty of the page 
for i in range(40):
	# Its return value is json data , Get it directly json Dictionaries 
    jsonData = requests.get(url + str(i+1)).json()
    # adopt jsonData['data']['list'] Get the beauty list 
    for j in jsonData['data']['list']:
    	# among j['avatar'] For the picture URL 
        imgUrl = j['avatar']
        # Send network request 
        resp = requests.get(imgUrl)
        # Create a picture file , And write the stream to the picture 
        img = open(dir + j['username'] + '.jpg', 'wb')
        img.write(resp.content)
 Copy code 

In this way, crawling for beautiful pictures is completed , If you remove the comments, it happens to be 10 Line code . Crawl the picture as follows : Crawling pictures

copyright notice
author[ZackSock],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011242469880.html

Random recommended