urequests
— Related functions of HTTP client¶
Before we used the socket library, this tool as a primer is still good, for understanding the basic concepts of some crawlers, its helpful to master it. After get started, we need to learn some more advanced content and tools to facilitate our crawling. Then this section briefly introduces the basic usage of urequests library.
Response class¶
-
class
urequests.
Response
(s)¶
The Response class object contains the server’s response to the HTTP request.
s
-ussl object
Method¶
-
urequests.
request
(method, url, data=None, json=None, headers={}, params=None, files=None)¶
Send an HTTP request to the server.
method
- HTTP method to useurl
- URL to senddata
- To append to the body of the request. If a dictionary or tuple list is provided, the form will be encoded.json
- json is used to attach to the body of the request.headers
- Dictionary of headers to send.params
- URL parameters attached to the URL. If a dictionary or tuple list is provided, the form will be encoded.files
- Used for file upload, the type is 2-tuple, which defines the file name, file path and content type. As follows,{‘name’, (file directory,content-type)}
-
urequests.
head
(url, **kw)¶
Send HEAD request and return Response object.
url
- Request object URL**kw
- The parameters of the request method.
-
urequests.
get
(url, **kw)¶
Send GET request and return Response object.
url
- Request object URL**kw
- Parameters of request method.
-
urequests.
post
(url, **kw)¶
Send POST request and return Response object.
url
- Request object URL**kw
- Parameters of request method.
-
urequests.
put
(url, **kw)¶
Send PUT request and return Response object.
url
- RRequest object URL**kw
- Parameters of request method.
-
urequests.
patch
(url, **kw)¶
Send PATCH request, return Response object.
url
- Request object URL**kw
- Parameters of request method.
-
urequests.
delete
(url, **kw)¶
Send a DELETE request. Return Response object。
url
- Request object URL**kw
- Parameters of request method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import urequests
from mpython import *
#连接网络
my_wifi = wifi()
my_wifi.connectWiFi('', '')
# 访问ip地址 api
r = requests.get("http://ip-api.com/json/")
print(r)
print(r.content) # 返回响应的内容
print(r.text) # 以文本方式返回响应的内容
print(r.content)
print(r.json()) # 返回响应的json编码内容并转为dict类型
# It's mandatory to close response objects as soon as you finished
# working with them. On MicroPython platforms without full-fledged
# OS, not doing so may lead to resource leaks and malfunction.
r.close()
|