使用get
方法获得响应对象Response,我们可以利用这个对象得到我们想要的任何信息。
# 实例引入
import requests
url = 'http://www.baidu.com'
response = requests.get(url)
print('1',response)
print('2',response.status_code) # 获得响应状态码
print('3',response.cookies) # 获得网页cookies信息
print('4',response.text) # 获得网页内容
print('5',type(response.text)) # 获得网页内容的类型
get
方法之外。Requests
还包括两种请求方法post
和delete
方法。import requests
response = requests.post('http://httpbin.org/post', data={'name' : 'xxxxxxx', 'age':18})
print(response.text)
response = requests.delete('http://httpbin.org/delete', data={'name' : 'xxxxxxx'})
print(response.text)
post
方法请求参数是通过data参数来传递的。delete
方法也通过data参数来传递请求参数。get
请求get
参数是以params
关键字参数传递的
#带参数的get请求
url1 = 'https://movie.douban.com/subject/4864908/comments?start=20&limit=20&sort=new_score&status=P'
import requests
data = {
'start': 20,
'limit': 40,
'sort': 'new_score',
'status': 'P',
}
url = 'https://movie.douban.com/subject/4864908/comment'
response = requests.get(url, params=data)
print(response.url)
JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
import requests
# ip = '8.8.8.8'
ip = input("请输入查询的IP:")
url = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" %(ip)
response = requests.get(url)
content = response.json()
print(content)
print(type(content))
# 获取二进制数据
import requests
url = 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=4f7bf38ac3fc1e17fdbf8b3772ab913e/d4628535e5dde7119c3d076aabefce1b9c1661ba.jpg'
response = requests.get(url)
print(response.text)
with open('github.png', 'wb') as f:
# response.text : 返回字符串的页面信息
# response.content : 返回bytes的页面信息
f.write(response.content)
import requests
# url = 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=4f7bf38ac3fc1e17fdbf8b3772ab913e/d4628535e5dde7119c3d076aabefce1b9c1661ba.jpg'
url = "http://gslb.miaopai.com/stream/sJvqGN6gdTP-sWKjALzuItr7mWMiva-zduKwuw__.mp4"
response = requests.get(url)
with open('/tmp/learn.mp4', 'wb') as f:
# response.text : 返回字符串的页面信息
# response.content : 返回bytes的页面信息
f.write(response.content)
在get
参数中田间header信息
import requests
url = 'http://www.cbrc.gov.cn/chinese/jrjg/index.html'
user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0'
headers = {
'User-Agent': user_agent
}
response = requests.get(url, headers=headers)
print(response.text)
print(response.status_code)
get
方法中加入header参数即可。import requests
# 上传的数据信息(字典存储)
data = {'file':open('github.png', 'rb')}
response = requests.post('http://httpbin.org/post', files=data)
print(response.text)
使用[http://httpbin.org/post]网站返回的值确定可上传文件
import requests
# 上传的数据信息(字典存储)
response = requests.get('http://www.csdn.net')
print(response.cookies)
for key, value in response.cookies.items():
print(key + "=" + value)
import requests
# 上传的数据信息(字典存储)
# 设置一个cookie: name='westos'
s = requests.session()
response1 = s.get('http://httpbin.org/cookies/set/name/villa')
response2 = s.get('http://httpbin.org/cookies')
print(response2.text)
get
方法中加入verify=False
可忽略证书验证# 忽略证书验证
import requests
url = 'https://www.12306.cn'
response = requests.get(url)
print(response.status_code)
print(response.text)
# 忽略证书验证
import requests
url = 'https://www.12306.cn'
response = requests.get(url, verify=False)
print(response.status_code)
print(response.text)
proxies=proxy
)/设置超时间(timeout=sec
)# 代理设置/设置超时间
import requests
proxy = {
'https': '171.221.239.11:808',
'http': '218.14.115.211:3128'
}
response = requests.get('http://httpbin.org/get', proxies=proxy, timeout=10)
print(response.text)