大佬我这边根据您的提示试了下,还是不行,空了能否帮忙看看[Python] 纯文本查看 复制代码 import os
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
import base64
import requests
from bs4 import BeautifulSoup
def get_response(url, timeout=10):
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Connection": "Keep-Alive",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
}
try:
response = requests.get(url, headers=headers,timeout=timeout)
response.raise_for_status() # 检查请求是否成功
response.encoding = 'utf-8'
return response
except requests.exceptions.Timeout:
print(f"请求超时:{url}")
except requests.exceptions.HTTPError as e:
print(f"HTTP错误:{e.response.status_code}, {url}")
except requests.exceptions.RequestException as e:
print(f"请求异常:{e}, {url}")
return None
def fetch_pic_urls(url):
if url.startswith('http'):
response = get_response(url)
html_content = response.text
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_content, 'html.parser')
# 查找所有包含data-xkrkllgl属性的img标签
img_tags = soup.find_all('img', attrs={'data-xkrkllgl': True})
# 提取并返回data-xkrkllgl属性的值
pic_urls = [img['data-xkrkllgl'] for img in img_tags]
return pic_urls
def decrypt_image_url(encrypted_url, key, iv):
# 将key和iv转换为字节串
key = bytes.fromhex(key)
iv = bytes.fromhex(iv)
# 将加密的URL从base64解码
encrypted_data = base64.b64decode(encrypted_url)
# 创建一个AES的CBC模式的解密器
cipher = AES.new(key, AES.MODE_CBC, iv)
# 解密数据
decrypted_padded = cipher.decrypt(encrypted_data)
# 去除填充
decrypted_data = unpad(decrypted_padded, AES.block_size)
# 将解密后的数据转换为字符串
decrypted_url = decrypted_data.decode('utf-8')
return decrypted_url
def download_image(url, save_dir='.', timeout=10):
response = get_response(url, timeout)
if response and response.status_code == 200:
# 从URL中提取文件名
filename = os.path.basename(url)
# 确保保存目录存在
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 拼接完整的文件路径
file_path = os.path.join(save_dir, filename)
with open(file_path, 'wb') as f:
f.write(response.content)
print(f'Image downloaded successfully to {file_path}.')
else:
print('Failed to download image.')
def main():
key = 'f5d965df75336270'
iv = '97b60394abc2fbe1'
url = 'xxxx'
encrypted_urls = fetch_pic_urls(url)
for encrypted_url in encrypted_urls:
decrypted_url = decrypt_image_url(encrypted_url, key, iv)
download_image(decrypted_url)
if __name__ == '__main__':
main()
|