-
#!/usr/bin/env python
-
# -*- coding: utf-8 -*-
-
# requests 是一个用python语言写的第三方库,在使用的时候,需要手动安装(pip install requests)
-
# 非常好用,基于urllib3
-
import requests
-
'''
-
requests 请求的函数
-
# requests.Request
-
# requests.request(method, url, **kwargs)
-
# requests.get(url, params, **kwargs)
-
# requests.post(url, data, json, **kwargs)
-
# requests.delete(url, **kwargs)
-
# requests.session()
-
'''
-
'''
-
reqiests 发起get请求的响应页面的方法
-
response = requests.get(url, params, **kwargs)
-
response.request
-
response.content # 获取的是二进制文本
-
response.text # 获取的是string字符串
-
response.headers # 获取请求头
-
response.encoding # 获取服务器的响应页面的编码格式
-
response.url # 获取浏览器向服务器发送的URL
-
response.status_code # 获取服务器向浏览器返回的状态吗
-
response.cookies # 获取服务器响应后的cookies
-
'''
-
'''
-
requests 发起post请求的响应页面的方法 (与get相同)
-
response = requests.post(url, data, json, **kwargs)
-
response.cookies
-
response.status_code
-
response.url
-
response.encoding
-
response.headers
-
response.text
-
response.content
-
response.request
-
'''
-
# response = requests.get(url='https://www.baidu.com/')
-
# Caused by SSLError(CertificateError
-
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
-
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'}
-
# 添加一个verify = False ,取消证书的验证
-
response = requests.get(url='https://www.12306.cn/mormhweb/',verify = False,headers = headers)
-
# 将获取的响应页面进行编码
-
response.encoding = 'utf-8'
-
# 获取响应页面的string文本
-
content = response.text
-
print(content)
requests (第三方库)基于urllib3 使用更加方便
最新推荐文章于 2023-11-15 13:45:50 发布