GET
普通的GET请求
import requests
respone = requests.get(url)
response.encoding = response.apparent_encoding
if response.status_code == 200:
return response.text
带参数的GET请求
import urllib.parse
import urllib.request
key_word= {"key":"vaule"}
data=urllib.parse.urlencode(key_word)
url = base_url + "?" + data
request = urllib.request.Request(url) # 创建请求对象
response = urllib.request.urlopen(request) # 获得服务器返回的数据
html = response.read() #处理数据
POST 请求
import urllib
from pprint import pprint
from urllib.parse import urlencode
from urllib.request import Request
import requests
from lxml import etree
url = "http://www.auto-mooc.com/api/v3/class/gettask"
key_word = {
"class_id": "EC209CF2D6F8EEFD06FCE3CFE8C20AEB",
"major_id": "SQ180902A",
"is_user": "0"
}
data = urlencode(key_word).encode() # 这里看情况进行转码
request = Request(url=url, data=data)
response = urllib.request.urlopen(request)
html = response.read()
pprint(html.decode()) # 这里对获取到的网页源码进行转码
评论区