URL Address Parameters
Before using the requests module to send a request, we need to review the url (Uniform Resource Locator) that we learned about earlier.
After you get the url address where the data is located, when you send a web request, the requested url contains two types of address parameters.Query parameterscap (a poem)Request Parameters。
When we crawl some special URLs, there will be some special parameters in the requested url, such as the following sites.
This kind of is a query parameter for URLs. Preceded by the URL, the?The binary data that follows is the query parameter.
Query String for URL(query string)Transmit some kind of data.
If you are building the URL by hand, that is, constructing the URL by splicing strings.
Then the data is placed in the URL as a key/value pair, followed by a question mark.
Example.
/i?q=%E9%A3%8E%E6%99%AF&src=srp
params keyword parameter
Requests Allows you to useparamskeyword arguments to a dictionary to provide them.
For example, if you want to pass key1=value1 and key2=value2 to /get, then you can use the following code.
import request params = {'q' : 'Scenery' , 'src' : 'srp'} response = ("https: ///i",params=params)
You can see that the URL has been encoded correctly by printing out the URL.
print()
# Print results
https : / //i?q=%E9%A3%8E%E6%99%AF&src=srp
take note of
Chinese characters are not supported by default in the url address, so the Chinese characters will be converted to url encoding form in the request.
The same query parameters can be found in the browser's packet grabber, located in theHeadersunder theQuery String Parametersin the following figure.
Request Parameters
There is a fundamental difference between request parameters and query parameters.
Request parameters are generally carried when sending a post request to submit a form data request to the server.
Note: The url address will not show the request parameters, only the query parameters. The request parameters are located in the Form Data under Headers in the browser's packet grabber tool as shown in the following packet grabber.
The data keyword parameter
Sending a POST request in the requests module is also a relatively easy operation; to accomplish this, simply pass adictionariesdo sth (for sb)data parameter。
Your data dictionary is automatically encoded into a form form when a request is made: the
data = {'key1' : 'value1','key2 ' : 'value2 '} response = ("/post",data=data)
Of course, Requests in the post method is only relative to the get method more than a data parameter, other parameters are similar, for example, we can also add a query string for the URL in the postparamsparameter, which can also be added like the get methodheadersparameters, etc.
to this article on the Python crawler requests module of the URL address of the parameters of the interpretation of the article is introduced to this, more related to Python requests module URL address parameter content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!