爬虫核心基础第四讲(requests库)

本文深入剖析了Python的requests库,详细介绍了如何使用该库进行HTTP请求,包括GET请求、定制头部信息、处理响应内容等。同时,通过实例演示了requests库在实际项目中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

requests库

源码分析

pip install requests

练习
两种,第二种是装饰器

class Person:
    def __init__(self,name):
        self._name = name

    def name(self):
        return self._name

p = Person('葫芦娃')
print(p.name())
class Person:
    def __init__(self,name):
        self._name = name

    @property
    def name(self):
        return self._name

p = Person('葫芦娃')
print(p.name)

模拟请求过程

模拟requests请求过程

import requests

r = requests.get('https://www.woyaogexing.com/touxiang/weixin/2020/937715.html')
# r.encoding = 'utf8'

# print(r)
# print(type(r))
# print(r.status_code)
# print(r.encoding)
# print(r.headers['content-type'])

# print(r.text)

html = r.text

with open('h.html','w',encoding='utf8') as f:
    f.write(html)
url = 'http://apis.juhe.cn/mobile/get?'

app_key = '申请的ApiKey'

# 要传递的参数

params = {
    'phone' : '1580133'
    'key': app_key
}

response = requests.get(url,params=params).json
print(response)

响应内容

响应图片内容

r = requests.get('https://img2.woyaogexing.com/2020/02/25/e95692a98d77416d9fa5ebe697456c6f!400x400.jpeg')
# print(r.headers['content-type'])
# print(r.content)

with open('test.jpeg','wb') as f:
    f.write(r.content)

响应api接口-ip地址查询

url = 'http://apidata.chinaz.com/CallAPI/ip?'

app_key = '这是我们要申请的ApiKey'

# 要传递的参数

params = {
    'ip' : '114.100.254.67',
    'key': app_key
}

response = requests.get(url,params=params)
print(response.headers['content-type'])
print(response.json())

定制头部信息

在这里插入图片描述
模拟

# 模拟
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
    'xm-sign': '3985ddedad15984c5a2a66ebf0cf36a0(39)1582618341558(13)1582618401259'
}

url = 'https://www.ximalaya.com/revision/play/v1/audio?id=231012348&ptype=1'

r = requests.get(url,headers=headers)

ret = r.text
print(ret)

响应头部信息

# 响应头部信息

url = 'https://www.woyaogexing.com/touxiang/weixin/2020/937715.html'

r = requests.get(url)

if r.status_code == 200:

    # print(r.text)

    h = r.headers
    print(h)

cookie和session

# cookie 和 session

url = 'https://www.woyaogexing.com/touxiang/weixin/2020/937715.html'

r = requests.get(url)

# print(r.cookies)
username = 'admin'
pwd = '123'

session = requests.session()

session.auth = (username,pwd)
print(session.auth)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值