日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

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

# -*- 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()

# 因?yàn)樵嫉膕ession.cookies 沒有save()方法,所以需要用到cookielib中的方法LWPCookieJar,這個類實(shí)例化的cookie對象,就可以直接調(diào)用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)

# 無論是否登錄成功,狀態(tài)碼一般都是 statusCode = 200

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

# 無論是否登錄成功,狀態(tài)碼一般都是 statusCode = 200

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

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

huihuSession.cookies.save()

def isLoginStatus():

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

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)

# 下面有兩個關(guān)鍵點(diǎn)

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

# 第二個是allow_redirects,如果不設(shè)置,session訪問時,服務(wù)器返回302,

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

# 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__":

# 從返回結(jié)果來看,有登錄成功

huihuLogin("xxxx", "xxxx")

isLogin1 = isLoginStatus()

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。