Python-爬虫之requests

Python-爬虫之urllib

一、requests返回的对象

# -------------------------------------
# 1个对象类型
print(type(response)) # <class 'requests.models.Response'>


# -------------------------------------
# 6个属性
# (1) 设置响应数据的编码格式
response.encoding = 'utf=8'

# (2) 以字符串形式,返回网页源码,此时还没有设置编码格式,返回数据会乱码
print(response.text)

# (3) 返回url
print(response.url)

# (4) 返回 字节型的二进制数据,一般不用
print(response.content)
print(response.content.decode('utf-8'))

# (5) 返回响应的状态码
print(response.status_code)

# (6) 获取响应头
print(response.headers)

二、通用的get请求

import requests
from requests import RequestException

try:
    # 1. 提供url
    url = 'https://www.bilibili.com'

    # 2. 提供url的参数
    data = {

    }

    # 3. 提供请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
    }

    # 4. 发送请求,获取响应数据
    # get方法:get(url, params=None, **kwargs):
    response = requests.get(url=url, data=data, headers=headers)

    # 5. 设置响应数据的编码格式
    response.encoding = 'utf-8'

    # 6. 获取数据
    con = response.text

    # 7. 处理数据
    print(con)

except RequestException:
    print('错误!!!')

三、通用的post请求

import json
import requests
from requests import RequestException

try:
    # 1. 提供url
    post_url = 'https://fanyi.baidu.com/sug'

    # 2. 提供url的参数
    data = {
        'kw': 'love'
    }

    # 3. 提供请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
    }

    # 4. 发送请求,获取响应数据
    # post请求
    response = requests.post(url=post_url, data=data, headers=headers)

    # 5. 设置响应数据的编码格式
    response.encoding = 'utf-8'

    # 6. 获取数据
    con = response.text

    # 7. 处理数据
    print(con)
    obj = json.loads(con)
    print(obj)

except RequestException:
    print('错误!!!')

四、通用的get请求使用代理池

import requests
from random import choice
from requests import RequestException

try:
    # 1. 提供url
    url = 'https://www.bilibili.com'

    # 2. 提供url的参数
    data = {

    }

    # 3. 提供请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
    }

    # 4. 提供代理池
    proxise_pool = [
        {'http': '47.242.66.236:4818'},
        {'http': '47.242.190.60:11573'},
    ]

    # 5. 发送请求,获取响应数据
    response = requests.get(url=url, data=data, headers=headers, proxies=choice(proxise_pool))

    # 6. 设置响应数据的编码格式
    response.encoding = 'utf-8'

    # 7. 获取数据
    con = response.text

    # 8. 处理数据
    print(con)

except RequestException:
    print('错误!!!')

五、通用的post请求使用代理池

import json
import requests
from random import choice
from requests import RequestException

try:
    # 1. 提供url
    post_url = 'https://fanyi.baidu.com/sug'

    # 2. 提供url的参数
    data = {
        'kw': 'love'
    }

    # 3. 提供请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
    }

    # 4. 提供代理池
    proxise_pool = [
        {'http': '47.242.66.236:4818'},
        {'http': '47.242.190.60:11573'},
    ]

    # 5. 发送请求,获取响应数据
    response = requests.post(url=post_url, data=data, headers=headers, proxies=choice(proxise_pool))

    # 6. 设置响应数据的编码格式
    response.encoding = 'utf-8'

    # 7. 获取数据
    con = response.text

    # 8. 处理数据
    print(con)
    obj = json.loads(con)
    print(obj)

except RequestException:
    print('错误!!!')

六、会话保持

  • requests.session():维持会话,可以让我们在跨请求时保存某些参数
session = requests.session()

# 下面两个请求,是在同一个会话中的
response = session.get()
response = session.post()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值