requests模块的初步使用

本文介绍如何使用Python的requests模块模拟登录人人网,并提供三种不同的实现方法。包括利用session对象发送带账号密码的POST请求,直接在Header中添加Cookie字符串,以及通过参数传递Cookie字典。

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

requests模块的简单使用

使用IP池的方法

requests模拟登录的三种方式

cookie和session的区别

cookie和session的优缺点

解决办法

准备很多账号和密码,那么就有很多cookie。携带一堆cookie进行请求,把cookie组成cookie池。

三种方式:

  • 实例化session,使用session发送post请求,在使用它获取登录后的页面(账号密码方式)
  • 在header中添加cookie键,值为cookie字符串
  • 在请求方法中添加cookie参数,接收字典形式的cookie

模拟登录1

import requests

session = requests.session()
post_url = "http://www.renren.com/PLogin.do"
post_data = {"email":"mr_mao_hacker@163.com", "password":"alarmchime"}
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"
}
# 使用session发送post请求,cookie保存在其中
session.post(post_url, data=post_data, headers=headers)
# 在使用session进行请求登录之后才能访问的地址
r = session.get("http://www.renren.com/327550029/profile", headers=headers)

# 保存页面
with open("renren1.html","w",encoding="utf-8") as f:
    f.write(r.content.decode())

模拟登录2

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36",
    "Cookie": "anonymid=kfaq9b1a-24dvxc; depovince=GUZ; _r01_=1; JSESSIONID=abczTQNg3VWlc_EiLoQsx; ick_login=696b9fd3-1078-441f-84b7-6719e64a2552; taihe_bi_sdk_uid=c390ad48b9bcdd599c0730e4c26c80c1; taihe_bi_sdk_session=048486a89c730d6954978ff3ae6299d1; _de=BF09EE3A28DED52E6B65F6A4705D973F1383380866D39FF5; t=1d6256f59cf5bcf00fc94ba4e05f69ae4; societyguester=1d6256f59cf5bcf00fc94ba4e05f69ae4; id=975149554; xnsid=55c297ac; jebecookies=98a53108-d6e5-4430-9566-310405448e8f|||||; ver=7.0; loginfrom=null; wp_fold=0"
}
# 在使用session进行请求登录之后才能访问的地址
r = requests.get("http://www.renren.com/327550029/profile", headers=headers)

# 保存页面
with open("renren2.html","w",encoding="utf-8") as f:
    f.write(r.content.decode())

直接使用cookie访问登录后的页面,使用场景:

  • cookie过期时间很长的网站
  • 在cookie过期之前能够拿到所有的数据,比较麻烦
  • 配合其他程序一起使用,其他程序专门获取cookie,当前程序专门请求页面

模拟登录3

import requests

# 在使用session进行请求登录之后才能访问的地址
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36",
}
cookies = "anonymid=kfaq9b1a-24dvxc; depovince=GUZ; _r01_=1; JSESSIONID=abczTQNg3VWlc_EiLoQsx; ick_login=696b9fd3-1078-441f-84b7-6719e64a2552; taihe_bi_sdk_uid=c390ad48b9bcdd599c0730e4c26c80c1; taihe_bi_sdk_session=048486a89c730d6954978ff3ae6299d1; _de=BF09EE3A28DED52E6B65F6A4705D973F1383380866D39FF5; t=1d6256f59cf5bcf00fc94ba4e05f69ae4; societyguester=1d6256f59cf5bcf00fc94ba4e05f69ae4; id=975149554; xnsid=55c297ac; jebecookies=98a53108-d6e5-4430-9566-310405448e8f|||||; ver=7.0; loginfrom=null; wp_fold=0"
cookies = {i.split("=")[0]:i.split("=")[1] for i in cookies.split("; ")}

r = requests.get("http://www.renren.com/327550029/profile", headers=headers, Cookies=cookies)

# 保存页面
with open("renren2.html","w",encoding="utf-8") as f:
    f.write(r.content.decode())

response.text和response.content的区别

requests保存图片

### 使用Python Requests库处理网页内容 #### 获取网页内容 为了使用Python中的`requests`库获取并处理网页内容,首先需要安装这个库。可以通过pip工具轻松完成这一操作[^1]。 ```bash pip install requests ``` 接着,在代码中导入`requests`模块,并定义目标网站的URL地址。利用`get()`方法发起GET请求以取得页面的数据。下面是一段简单的示例程序用于展示如何执行此过程[^2]: ```python import requests url = "https://www.example.com/" response = requests.get(url) html_content = response.text print(html_content) ``` 这段代码会打印出所访问站点返回的HTML源码字符串形式的内容。 #### 处理响应对象属性 除了`.text`外,还可以通过其他几个重要的属性来进一步解析服务器响应的信息。例如,可以查看状态码(`status_code`)确认请求是否成功;也可以读取头部信息(headers),这些都保存在Response类实例之中[^3]。 ```python # 检查HTTP请求的状态码 if response.status_code == 200: print("Request was successful.") else: print(f"Failed with status code {response.status_code}") # 查看响应头部分 for key, value in response.headers.items(): print(key + ": " + value) # 输出实际接收到的内容长度 content_length = int(response.headers['Content-Length']) print(f"The size of the content is approximately {content_length} bytes.") ``` #### 设置自定义请求参数 当向Web服务发送请求时,可能还需要传递额外的信息给对方服务器,比如查询参数(query parameters)或者表单数据(form data)[^4]。这里给出一个例子说明怎样做: ```python params = {'key1': 'value1', 'key2': 'value2'} data = {'field1': 'hello', 'field2': 'world'} response_with_params = requests.post('http://httpbin.org/post', params=params, data=data) print(response_with_params.json()) ``` 以上就是关于如何运用Python `requests`库来进行基本的网页内容抓取以及初步处理的方法介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值