requests库的get请求(加上head,加上get参数请求)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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的请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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传参的请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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())

get带有代理的请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#coding:utf-8
import requests
 
# 构建一个url
url = 'http://www.itcast.cn'
# 构建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'
        }
# 构建代理
# proxies = {
#     "http": "http://106.14.51.145:8118",
#     "https": "https://106.14.51.145:8118",
# }
# 付费代理
proxies = {
    "http": "http://morganna_mode_g:ggc22qxp@117.48.199.230:16816",
    "https": "https://morganna_mode_g:ggc22qxp@117.48.199.230:16816",
}
 
# 发送请求
response = requests.get(url,headers=headers,proxies=proxies)
 
 
#? 如何验证代理是否使用成功<br># 运用超时抛出异常来判断是否成功,一般0.5-1秒<br># response = requests.get(url, timeout=3)

  

 

posted @   安迪9468  阅读(1408)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示