The Python standard library provides modules such as Urllib for HTTP requests, but its API is too slag. It was created for another era, another internet. It requires a huge amount of work, even covering a variety of methods, to accomplish the simplest task.
Send a GET request
Import URLLIB.REQUESTF = Urllib.request.urlopen (' http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/ qqcheckonline?qqcode=424662508 ') result = F.read (). Decode (' Utf-8 ')
Send a GET request that carries a request header
Import urllib.requestreq = urllib.request.Request (' http://www.example.com/') req.add_header (' Referer ', '/HTTP/ www.python.org/') R = Urllib.request.urlopen (req) result = F.read (). Decode (' Utf-8 ')
Click to view the official documents for more information
Requests is a Python-developed HTTP library using the Apache2 Licensed license, which is highly encapsulated on the basis of Python's built-in modules, making it much better for Pythoner to make network requests. With requests, you can easily do whatever your browser can do.
1. Installing the module
PIP3 Install Requests
2. Using the module
GET request
# 1, no parameter instance import requests ret = Requests.get (' Https://github.com/timeline.json ') print (ret.url) print (Ret.text) # 2 , parameter instance import requests payload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}ret = Requests.get ("Http://httpbin.org/get", params= Payload) print (ret.url) print (Ret.text)
POST request
# 1, Basic post instance import requests payload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}ret = Requests.post ("Http://httpbin.org/post", data=payload) print (Ret.text) # 2, send request header and data instance import requestsimport json url = ' https://api.github.com/some/ Endpoint ' payload = {' Some ': ' data '}headers = {' Content-type ': ' Application/json '} ret = requests.post (URL, data= Json.dumps (payload), headers=headers) print (ret.text) print (ret.cookies)
Other requests
Requests.get (URL, Params=none, **kwargs) requests.post (URL, Data=none, Json=none, **kwargs) requests.put (URL, data= None, **kwargs) requests.head (URL, **kwargs) requests.delete (URL, **kwargs) requests.patch (URL, data=none, **kwargs) Requests.options (URL, **kwargs) # The above methods are constructed on the basis of this method Requests.request (method, URL, **kwargs)
More requests module related documents Click to view
3. HTTP requests and XML instances
Example: Checking whether QQ account is online
Import urllibimport requestsfrom xml.etree import elementtree as et# send HTTP requests using built-in module urllib, or XML format content "" "F = urllib.request . Urlopen (' http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508 ') result = F.read (). Decode (' Utf-8 ') "" "# sends an HTTP request using a third-party module requests, or XML-formatted content R = Requests.get (' http://www.webxml.com.cn// webservices/qqonlinewebservice.asmx/qqcheckonline?qqcode=424662508 ') result = r.text# Parse XML format content node = ET. XML (Result) # Gets the content if Node.text = = "Y": print ("online") Else: print ("Offline")
Example: View train docking information
Import urllibimport requestsfrom xml.etree import elementtree as et# send HTTP requests using built-in module urllib, or XML format content "" "F = urllib.request . Urlopen (' Http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode? traincode=g666&userid= ') result = F.read (). Decode (' Utf-8 ') "" "# Use third-party module requests to send HTTP requests, or XML format content r = Requests.get (' Http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode? traincode=g666&userid= ') result = r.text# parsing XML format content root = ET. XML (Result) for node in Root.iter (' Traindetailinfo '): print (Node.find (' trainstation '). Text,node.find (' StartTime '). Text,node.tag,node.attrib)
Python's requests module