Python爬虫:请求页面基本库(二)request

request

用urllib去处理网页验证和Cookies时,需要写Opener和Handler来处理,很不方便,这里我们学习更为强大的库request

get()

实例:

import requests #导入requests

html = requests.get('https://www.csdn.net/')#使用get方法获取页面信息
print(html.text)#调取text属性查看页面代码

添加参数使用param+字典

import requests  # 导入requests

data = {
    'jl': '765',
    'kw': 'python',
    'kt': '3'
}
html = requests.get('https://sou.zhaopin.com/',params=data)  # 添加参数
print(html.text)  # 调取text属性查看页面代码

添加headers使用headers+字典

import requests  # 导入requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'

}

data = {
    'jl': '765',
    'kw': 'python',
    'kt': '3'
}
html = requests.get('https://sou.zhaopin.com/',headers=headers,params=data)  # 添加参数
print(html.text)  # 调取text属性查看页面代码

高级用法

cookies设置,代理设置等

Cookies

获取cookies:

import requests  # 导入requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'

}

data = {
    'jl': '765',
    'kw': 'python',
    'kt': '3'
}
html = requests.get('https://blog.csdn.net/qq_40966461/article/details/104974998',headers=headers,params=data)  # 添加参数
print(html.cookies)  # 调取text属性查看页面代码
for key,value in html.cookies.items():
    print(key+'='+value)

很简单,直接获取cookies属性即可

维持会话Session()

在requests中,如果直接利用get()或post()等方法可以做到模拟网页的请求,但是这实际上时相当于不同的会话,相当于用了两个浏览器打开了不同的页面,这时需要用session对象来维护对话

import requests  # 导入requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'

}
data = {
    'jl': '765',
    'kw': 'python',
    'kt': '3'
}
html = requests.Session().get('https://blog.csdn.net/qq_40966461/article/details/104974998',headers=headers,params=data)  # 添加参数
print(html.cookies)  # 调取text属性查看页面代码
for key,value in html.cookies.items():
    print(key+'='+value)

调用requests模块中get方法时先创建一个Session对象

SSL证书验证
import requests  # 导入requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'

}

response  = requests.get('http://www.12306.cn',headers=headers,verify = False)
print(response.status_code)

verify=False即可

代理设置
import requests  # 导入requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'

}
proxies = {
    "http":"http://183.166.132.176",
    "https":"https://183.166.132.176"
}

response  = requests.get('http://www.12306.cn',headers=headers,proxies=proxies,verify = False)
print(response.status_code)

添加proxies即可,代理可以搜索快代理

超时设置

加参数timeout= 1

身份认证

get中添加参数 auth=(‘username’,‘password’)
OAuth认证方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值