一、requests.get()使用
#Python 的标准库 urllib 提供了大部分 HTTP 功能,但使用起来较繁琐。
#通常,我们会使用另外一个优秀的第三方库:Requests,它的标语是:Requests: HTTP for Humans。
import requests
reponse=requests.get("https://www.baidu.com/")
reponse.encoding='utf-8'
# 返回状态码
print(reponse.status_code)
# 打印网页的内容
print(reponse.text)
二、请求头
import requests
#构建请求头,模拟浏览器访问
header={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
}
reponse=requests.get('http://www.baidu.com/',headers=header)
print(reponse.content.decode('utf-8'))
三、post请求
import requests
#包装要发送的数据
data={
'name':'zhang',
'age':'18'
}
header={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
}
#调用post()方法进行post请求
reponse=requests.post('http://httpbin.org/post',data=data,headers=header)
print(reponse.text)
返回结果如下:传递的name和age的值已经被接收
{
"args": {},
"data": "",
"files": {},
"form": {
"age": "18",
"name": "zhang"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "17",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36"
},
"json": null,
"origin": "180.173.37.153",
"url": "http://httpbin.org/post"
}
四、代理的使用
import requests
proxy={
'http':'122.114.31.177'
}
reponse=requests.get('http://www.baidu.com/',proxies=proxy)
print(reponse.status_code)
五、超时设置
from requests.exceptions import Timeout
try:
reponse=requests.get('http://www.baidu.com/',timeout=0.001)
print(reponse.status_code)
except Timeout:
print("访问超时")