试试用 session 看看
import requests
import urllib.parse
from bs4 import BeautifulSoup
# 输入的Cookie字符串
cookie_str = "cookies 这里搞个演示"
# 解码Cookie字符串
decoded_cookie = urllib.parse.unquote(cookie_str)
# 分割Cookie
cookie_parts = decoded_cookie.split("; ")
# 创建一个字典来存储Cookie项
cookie_data = {}
for part in cookie_parts:
key, value = part.split("=")
cookie_data[key] = value
login_url = 'https://hr.xxxx.com/portal/index' # 页面URL
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'}
sess = requests.Session()
response = sess.get(login_url, cookies=cookie_data, headers=headers)
if response.status_code == 200:
response.encoding = response.apparent_encoding # 文字解码
soup = BeautifulSoup(response.text, 'html.parser')
app = soup.find('a', attrs={'title': "个人考勤查询"}).get('href')
url2 = 'https://hr.xxxx.com/' + app
response = sess.get(url2, headers=headers)
response.encoding = response.apparent_encoding # 文字解码
soup = BeautifulSoup(response.text, 'html.parser')
times=soup.find_all('span')
else:
print('网页登录失败!')
|