首先使用requests或Selenium模拟登录并维持会话,1.通过Session获取csrf token并提交登录表单,2.对JS渲染页面用Selenium操作浏览器登录并注入cookies,3.后续请求复用同一Session对象访问受保护内容。

抓取需要登录的网站,关键在于模拟登录过程并维持会话状态。Python爬虫通过携带有效的身份凭证(如 cookies 或 token)请求受限页面,从而获取受保护内容。下面介绍常用方法和实现步骤。
大多数登录网站使用表单提交用户名和密码。通过分析登录接口,用 requests 发送 POST 请求,并保存返回的 cookies,后续请求即可携带这些凭证。
基本流程如下:
import requests
from bs4 import BeautifulSoup
<p>session = requests.Session()</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><h1>第一步:获取登录页和 csrf token</h1><p>login_url = '<a href="https://www.php.cn/link/d9976f1c2c0c972d1cee0c3647cbd194">https://www.php.cn/link/d9976f1c2c0c972d1cee0c3647cbd194</a>'
res = session.get(login_url)
soup = BeautifulSoup(res.text, 'html.parser')
csrf_token = soup.find('input', {'name': 'csrf'})['value']</p><h1>第二步:提交登录表单</h1><p>login_data = {
'username': 'your_username',
'password': 'your_password',
'csrf': csrf_token
}
session.post(login_url, data=login_data)</p><h1>第三步:访问受限页面</h1><p>protected_page = session.get('<a href="https://www.php.cn/link/fad68ee497f1cf9108b630e7ce630e6c">https://www.php.cn/link/fad68ee497f1cf9108b630e7ce630e6c</a>')
print(protected_page.text)
有些网站前端由 JavaScript 动态渲染,表单提交通过 Ajax,且登录状态依赖本地存储(如 localStorage)。这种情况下,requests 难以直接模拟。推荐使用 Selenium 驱动真实浏览器操作。
主要优势:
from selenium import webdriver
import time
<p>driver = webdriver.Chrome()
driver.get('<a href="https://www.php.cn/link/d9976f1c2c0c972d1cee0c3647cbd194">https://www.php.cn/link/d9976f1c2c0c972d1cee0c3647cbd194</a>')</p><h1>填写表单并提交</h1><p>driver.find_element_by_name('username').send_keys('your_username')
driver.find_element_by_name('password').send_keys('your_password')
driver.find_element_by_tag_name('form').submit()</p><p>time.sleep(3) # 等待登录完成</p><h1>将 cookies 注入 requests session</h1><p>session = requests.Session()
for cookie in driver.get_cookies():
session.cookies.set(cookie['name'], cookie['value'])</p><h1>后续可用 session 抓取内容</h1><p>res = session.get('<a href="https://www.php.cn/link/6499e19d47d7cbd3302a26fdb40d0b41">https://www.php.cn/link/6499e19d47d7cbd3302a26fdb40d0b41</a>')
print(res.text)</p><p>driver.quit()
Python 的 requests.Session() 能自动管理 cookies,是维持登录状态的核心工具。只要在同一个 session 中完成登录和后续请求,服务器就会识别为已认证用户。
注意事项:
登录类网站通常有反爬策略,需适当规避:
基本上就这些。核心是模拟真实用户行为,保持会话连贯性。对于简单表单用 requests 足够,复杂交互推荐 Selenium。只要能稳定登录,抓取受限内容就不成问题。
以上就是Python爬虫如何抓取需要登录的网站_Python爬虫模拟登录后抓取受限内容方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号