爬虫库的基本使用(3)

request库 --第三方库

Requests是用python语言编写的,基于urllib,但是它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。

#安装requests
pip install requests

基本使用

发送get请求:

resp=requests.get('http://www.baidu.com')
import requests

#添加headers和查询参数
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
kw={'wd':'中国'}

#params 接收一个字典或字符串的查询参数,字典类型自动转换为url编码,不需要使用urlencode()
resp=requests.get('https://www.baidu.com/s',headers=headers,params=kw)

print(resp)

#查询响应内容
# print(resp.text)    #返回unicode格式数据
# print(resp.content.decode('utf-8')) #返回字节流数据 可以手动编码

print(resp.url)
print(resp.encoding)

发送post请求:

#主要是添加了data参数(字典类型)
resp=requests。post('http://www.baidu.com',data=data)
#请完善data中的数据
import requests
url='https://i.meishi.cc/login.php?redirect=https%3A%2F%2Fwww.meishij.net%2F'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}

data={'redirect': 'https://www.meishij.net/',
    'username': '',
    'password': ''}

resp=requests.post(url,headers=headers,data=data)
print(resp.text)

使用代理

#主要添加proxies参数
import requests

proxy={'http':'123.169.118.136:9999'}

url='http://www.httpbin.org/ip'

resp=requests.get(url,proxies=proxy)
print(resp.text)

使用cookie值登录

session: 使用requests,也要达到共享cookie的目的

#获取cookie信息
import requests
resp=requests.get('https://www.baidu.com')
print(resp.cookies)
print(resp.cookies.get_dict())
#共享cookie
post_url='https://i.meishi.cc/login.php?redirect=https%3A%2F%2Fwww.meishij.net%2F'
post_data={'username':'1097566154@qq.com',
    'password':'wq15290884759.'}
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}

#登录
session=requests.session()
session.post(post_url,headers=headers,data=post_data)

#访问个人网页
url='https://i.meishi.cc/cook.php?id=13686422'
resp=session.get(url)
# print(resp.text)

处理不信任的SSL证书

#在get中添加verify=False
import requests
url='https://inv-veri.chinatax.gov.cn/'
resp=requests.get(url,verify=False)

print(resp.content.decode('utf-8'))
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值