首页 技术 正文
技术 2022年11月20日
0 收藏 951 点赞 4,626 浏览 3323 个字

1 发送get请求获取页面

 import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)

2 发送post请求获取页面

 import requests # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.post(url=url)
# 3 获取响应内容文本 两种方法
html1 = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html1) response.encoding='utf8'
html2 = response.text # 用response.text 会自动选择一种方式解码 有时候会乱码,要提前设置response.encoding
print(html2)

3 伪装浏览器,携带报头

 import requests # 伪装我们的报文头,加上Use-Agent 伪装成浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)

4 携带数据 (比如 发送请求去登陆)

 import requests # 如果伪装登录,可以传送一个字典类型数据
data = {
'''这里放入需要的key:value'''
}
# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
# get请求用params 相当于在url后面拼接key=value&key=value
response = requests.get(url=url,params=data)
# post用data传入参数 携带post的数据
response = requests.post(url=url,data=data)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)

5 代理

import requests
# 将代理的服务器放入这里,key为协议类型 value为代理的ip和端口
# 发送https或者http请求会根据不同代理ip选择 为我们发送请求
proxies = {
'http':'http://127.0.0.1:80',
'https':'https://127.0.0.1:80'
}# 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,proxies=proxies)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)

6 携带cookie

 import requests # 如果要带着cookie字典 可以传入cookie,也可以放在报文头当中
cookies = {
#'key':'value',
} # 或者将cookie放在报文头当中
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
# 如果要带着cookie 可以传入cookie,也可以放在报文头当中
#'Cookie':'这里放入cookie'
} # 1 要爬取的页面地址
url = 'http://www.baidu.com'
# 2 发送get请求 拿到响应
response = requests.get(url=url,cookies=cookies)
#response = requests.get(url=url,headers=headers)
# 3 获取响应内容文本 两种方法
html = response.content.decode() #response.content为bytes类型,decode() 将它转换为utf8
print(html)

7 保持session 帮我们保存response中的session

 import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url)
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)

8 设置连接超时时间

 import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,timeout = 3) # 3秒时间为超时时间
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)

9 设置ssl校验 对方https协议合法性是否忽略

 import requests
# 获取一个session对象为我们发送请求 用法与requests对象相同
session = requests.session() url = 'http://www.baidu.com'
#保持session发送请求
response = session.get(url=url,verify=False) # 不校验ssl 如果对方https协议不合法,我们忽略 继续请求
# 获取页面
html = response.content.decode()
print(html)
#查看session
print(response.cookies)

10 重新连接次数

 import requests
from retrying import retry @retry(stop_max_attempt_number=3) # 设置超时重新连接 次数3
def get( url ):
response = requests.get(url=url,timeout=3)
return response.content.decode() url = 'http://www.baidu.com'
html = get(url)
print(html)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,989
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,131
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,842