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 use requests in python

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to use requests in python. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

First of all, you need to install the requests library

Installation code: pip3 install requests

If you do not install pip3, please install Baidu by yourself. if you have reached a cooperation with Baidu, you can go to Baidu, free of charge.

Let's get to the point. Let's take a look at the power of requests.

1 get request

Is it simple and rough? It's much more comfortable than the last one.

What are the attributes? I don't know, dir ()?

A brief introduction to several attributes:

Status_code: status code

Url:url

Text: content

Cookies: that's cookies

Let's try to add some parameters with the get request, just use the params parameter

The returned result:

Add headers:

Take Zhihu as an example: if Zhihu does not add headers, 500 errors will be reported.

Return the result:

2 post request and add parameters

Return the result:

3. Try to log in to Zhihu with cookies

In fact, there are also operations on cookies in urllib, but it is very troublesome. Compared with requests, it is much simpler.

Open developer mode when logging in to Zhihu

As shown in the figure:

Copy the cookie value and add the cookie value to the headers:

You will find that you can already see the results after login!

At this point, you have to consider that there will be more than one request for each page crawl, so it is troublesome to add cookies for each request, so if you have session persistence, you will use requests.Session () to set it. I won't talk about it in this section, which will be mentioned later.

4. In this section, we will simply use what we have learned to download pictures and music.

Download the picture:

First of all, find a picture website: I choose Qiantu Network to find a picture and right-click to copy the link address.

And write a piece of code like this

Open () can operate on text, pictures, etc.

The first parameter of open () is the location and name of your picture, and the second parameter is the operation that can be performed, such as w is written, r is read, wb is the operation on binary files, our pictures and music are all binary files, after running, you will find that there is already a picture in this folder, so there is no screenshot here. It is worth noting that if you want to print text, it will be a piece of garbled code. Because it is a picture, and content will start with b, indicating that it is of type bytes, which is a string of binary data.

Download music:

I chose NetEYun Music. (it's a little more troublesome to find the address of the music here.) Open NetEyun, search for the music you want to download, and then open developer mode and click to play.

As shown in the figure:

Find the url of the song, that is, the source address of the file, which should be a temporary address with a time limit. Copy this address and open it on a new page like this:

Then change the requested address to the address you copied, and change the end of the stored address to the end of mp3.

The above is how to use requests in python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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