SoFunction
Updated on 2024-10-30

python crawler tutorial for beginners - using requests to build the Zhihu API (III)

preamble

In the crawler series of articlesElegant HTTP library requestsThis time we use requests to build a Zhihu API with functions such as sending private messages, liking articles, following users, etc. Because any function that involves user operations requires login, it is recommended that you understand thePython simulation of Zhihua login Now let's say you know how to use requests to simulate a KnowledgeBase login. Now let's say you already know how to simulate a know login with requests.

analysis of ideas

The process of sending a private message is that the browser sends an HTTP request to the server, the request message includes the request URL, the request header Header, and the request body Body, as long as the information is clear, then it is easy to use requests to simulate the browser to send a private message.

Open Chrome, find a random user, click Send Private Message, and trace the web request process for the private message.

Let's start with the request headers

The request header has the cookie login information, and there is also an authorization field, which is used for user authentication, as well as a cookie (I've mosaicked it to prevent the cookie from leaking), which must be carried in the request.

Let's look at the request URL and request body again

The request URL is /api/v4/messages, the request method is POST, and the request body is

{"type":"common","content":"Hello, I'm Pythoner.","receiver_hash":"1da75b85900e00adb072e91c56fd9149"}

The body of the request is a json string, the type and content are well understood, but what the receiver_hash is is not known and needs to be determined, but you should be able to guess that it's something like a user id.

So now the question is, how to find the user's id through the URL of the user's homepage? In order to completely simulate the entire process of private messaging, I specifically registered a Zhihu Xiaoxiao.

If you do not have extra cell phone numbers on hand, you can use Google to search for "receive sms online", the Internet provides a lot of free online receive sms cell phone number, I registered small homepage:/people/xiaoxiaodouzi

Tried following the trumpet first, then found the trumpet in the list of those I follow, and when I moused over the trumpet's avatar, I found an HTTP network request.

The request url is /api/v4/members/xiaoxiaodouzi, the back part of this url "xiaoxiaodouzi" corresponds to the back part of the homepage URL of Xiaoxiaodouzi, which is called url_token.

Return data of the interface

{ 
 ...
 "id":"1da75b85900e00adb072e91c56fd9149",
 "favorite_count":0,
 "voteup_count":0,
 "commercial_question_count":0,
 "url_token":"xiaoxiaodouzi",
 "type":"people",
 "avatar_url":"/v2-ca13758626bd7367febde704c66249ec_is.jpg",
 "is_active":1492224390,
 "name":"\u6211\u662f\u5c0f\u53f7",
 "url":"/api/v4/people/1da75b85900e00adb072e91c56fd9149",
 "gender":-1
 ...
}

We can clearly see that there is an id field, and as we guessed before, the receiver_hash field inside the private message is the user's id.

code implementation

To this point we put the private messaging function of the ideas clear, the code implementation is a matter of water to the end of the thing.

user information

In order to get the receiver_hash dictionary needed for the private messaging interface, we first need to get the user information, which contains the id value used.

@need_login
def user(self, url_token):
 """
 Get user information,
 :param url_token.
 url_token is the last part of the user's homepage url.
 Example: /people/xiaoxiaodouzi
 url_token is xiaoxiaodouzi
 :return:dict
 """
 response = self._session.get((url_token))
 return ()

Send Private Message

@need_login
def send_message(self, user_id, content):
 """
 Send a private message to the specified user
 :param user_id: user_id
 :param content: content of the private message
 """
 data = {"type": "common", "content": content, "receiver_hash": user_id}
 response = self._session.post((), json=data)
 data = ()
 if ("error"):
 ("Private message failed to send, %s" % ("error").get("message"))
 else:
 ("Sent successfully.")
 return data

The above two methods are placed inside a class called Zhihu, and I've only listed the key code, which involves @need_login, a user authentication decorator that indicates that the method needs to be logged in to operate. If you are careful, you may notice that I didn't specify the Header field in each request, because I initialized it in the __init__.py method.

def __init__(self):
 self._session = ()
 self._session.verify = False
 self._session.headers = {"Host": "",
    "Referer": "/",
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36'
      ' (KHTML, like Gecko) Chrome/56.0.2924.87',
    }
 self._session.cookies = (filename=cookie_filename)
 try:
 self._session.(ignore_discard=True)
 except:
 pass

invoke execution

from zhihu import Zhihu

if __name__ == '__main__':
 zhihu = Zhihu()
 profile = ("xiaoxiaodouzi")
 _id = ("id")
 zhihu.send_message(_id, "Hello, greetings from the Zen of Python.")

After the execution was completed, the trumpet successfully received the private message I sent.

Finally, we can follow a similar idea to put in place features such as following users, likes, etc.

Source code address:/lzjun567/zhihu-api 

A download online download online:http://xiazai./201705/yuanma/zhihu-api().rar

summarize

The above is all about this article, I hope that the content of this article for everyone to learn or use python can bring some help, if there are questions you can leave a message to exchange, thank you for my support.