Python Extra: Automatically download the avatar pictures of all your fans (requests + urllib)

Hello, everyone, my name is wangzirui32, today we will learn how to crawl and download the avatar pictures of all your fans.

1. Analyze the fan list

1.2 Data interface

After my analysis, I found that the fan list is generated by loading the js script, so it will inevitably access a data interface. After searching, this interface is:

https://blog.csdn.net//phoenix/web/v1/fans/list?blogUsername=你的用户名

You can get all the fan data of a certain user by visiting this interface.

1.3 Analysis data interface

Here I put the picture directly, if you don’t understand, you can discuss it in the comment area:
analysis

2. Write the code

First, you need to create a picture folder in the directory of the program file , and then write the code according to the above analysis: (read the comments if you don’t understand)

from requests import get
from json import loads
from urllib.request import urlopen

# 伪造请求头
headers = {
    
    
           "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36",
		   "Host": "blog.csdn.net"
}

# 参数定义 可以把blogUsername改为你的用户名(Tips:是用户名不是昵称!)
params = {
    
    "blogUsername": "wangzirui32"}

# 请求数据
url = "https://blog.csdn.net//phoenix/web/v1/fans/list"
r = get(url, params=params, headers=headers)

# 使用loads加载数据
fans_data = loads(r.text)['data']['list']

# 用来统计成功下载的粉丝头像的数量
save_fans_num = 0

# 遍历所有粉丝数据
for i in fans_data:
	# 文件名为:当前粉丝的昵称.png
    with open("picture/" + i['nickname'] + ".png", "wb") as f:
    	# 防止发生错误
        try:
            f.write(urlopen(i["userAvatar"]).read())
        except Exception:
        	# 如果错误,就提示下载失败
            print(i['nickname'] + "的头像下载失败!")
        else:
        	# 如果没有报错,则提示下载完成,并将save_fans_num+1
            print(i['nickname'] + "的头像已经下载完毕!")
            save_fans_num += 1

print("头像下载完毕!")
print("一共下载了" + str(save_fans_num) + "个粉丝头像")

3. Show the results

At present, I have 15 fans, so I should download 15 photos without errors, run the code, and the result is really like this:
Result displaysee if you are there!

Write at the end

Some bloggers have a lot of fans, some even thousands or tens of thousands, and the program takes a long time to run to complete the crawl. Here I suggest using multi-process crawlers or multi-coroutine crawlers to solve the problem of long time.


Well, today’s class is over, if you are interested, you can like and collect, bye bye!

Guess you like

Origin blog.csdn.net/wangzirui32/article/details/114233799