日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬带用户名密码的网页_python爬虫用户名密码登录POST

發布時間:2025/3/19 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬带用户名密码的网页_python爬虫用户名密码登录POST 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

# -*- 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/",

'User-Agent': userAgent,

}

def huihuLogin(account, password):

#

print ("開始模擬登錄嘻嘻嘻")

postUrl = "http://hh.haiper.com.cn/w/wander/user/login/"

postData = {

"username": account,

"password": password,

}

# 使用session直接post請求

responseRes = huihuSession.post(postUrl, data = postData, headers = header)

# 無論是否登錄成功,狀態碼一般都是 statusCode = 200

#responseRes = requests.post(postUrl, data = postData, headers = header)

# 無論是否登錄成功,狀態碼一般都是 statusCode = 200

print(f"statusCode = {responseRes.status_code}")

#print(f"text = {responseRes.text}")

huihuSession.cookies.save()

def isLoginStatus():

# 通過訪問個人中心頁面的返回狀態碼來判斷是否為登錄狀態

for i in range(2131,2134):

routeUrl = "http://hh.haiper.com.cn/w/bench/extend/health/trade/all?nickname=&type=&gender=&level=&range%5Bstart%5D=2014-11-11+14%3A57&range%5Bend%5D=2018-06-06+14%3A57&page="+str(i)

# 下面有兩個關鍵點

# 第一個是header,如果不設置,會返回500的錯誤

# 第二個是allow_redirects,如果不設置,session訪問時,服務器返回302,

# 然后session會自動重定向到登錄頁面,獲取到登錄頁面之后,變成200的狀態碼

# allow_redirects = False 就是不允許重定向

try:

responseRes = huihuSession.get(routeUrl, headers = header, allow_redirects = False)

result = responseRes.text

except:

continue

start = result.find('

')

result = result[start:]

#print (result)

for j in range(1,16):

start = result.find('擦擦擦圖片')

if start==-1:

break

else:

result = result[start:]

start = result.find('src="')

result = result[start+5:]

end = result.find('" class="img-rounded"')

imgpath = result[:end]

print (imgpath)

if imgpath=='/attachment/':

continue

randomname = datetime.now().strftime("%Y%m%d_%H%M%S") + str(random.randint(1,100))+'.jpg'

try:

urllib.request.urlretrieve(imgpath,'./擦擦擦/'+randomname)

except:

continue

print (i)

print(f"isLoginStatus = {responseRes.status_code}")

#print(f"text = {responseRes.text}")

if responseRes.status_code != 200:

return False

else:

return True

if __name__ == "__main__":

# 從返回結果來看,有登錄成功

huihuLogin("xxxx", "xxxx")

isLogin1 = isLoginStatus()

print(f"is login huihu = {isLogin1}")

總結

以上是生活随笔為你收集整理的python爬带用户名密码的网页_python爬虫用户名密码登录POST的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。