首页 > 其他 > 详细

requests库的get请求

时间:2018-01-17 23:57:06      阅读:260      评论:0      收藏:0      [点我收藏+]
#coding:utf-8
# 导入requests
import requests

# 构建url
url = ‘http://www.baidu.com‘

# 发送请求,获取响应
# response = requests.get(url)
response = requests.head(url)

# 检查状态码
# print (response.status_code)

# 检查url
# print (response.url)

# 检查请求头
# print (response.request.headers)

# 检查响应头
# print (response.headers)

# 检查源码
# print (response.content)
# print (response.content.decode())
#
# response.encoding=‘utf-8‘
# print (response.text)
# print (response.encoding)

  带headers的请求

#coding:utf-8
import requests
import time

# 构建url
url = ‘http://www.baidu.com‘

# 构建请求头
headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36‘
}


# 发送请求
response = requests.get(url, headers=headers)
print (len(response.content))

time.sleep(2)
response1 = requests.get(url)
print (len(response1.content))

  

带get传参的请求

#coding:utf-8
import requests

# 构建url
url = ‘https://www.baidu.com/s‘
# 构建headers
headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36‘
}

# 构建参数
params = {
    "wd": "深圳"
}

# 发送请求
response = requests.get(url, headers=headers, params=params)

# 验证url
# print(response.url)

with open(‘baidu.html‘,‘w‘)as f:
    f.write(response.content.decode())

  

 

requests库的get请求

原文:https://www.cnblogs.com/andy9468/p/8306749.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!