虎牙小姐姐合集
作者:互联网
#-*- codeing = utf-8 -*-
#@Time:2021/3/12 0012 22:21
#@Author:权倾天下
#@File:爬取虎牙小姐姐照片.py
#@Software:PyCharm
import requests
import os
import json
import time
from random import randint
def main():
url = 'https://www.huya.com/cache.php'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
for i in range(5):
page_num = str(i)
params = {
'm': 'LiveList',
'do': 'getLiveListByPage',
'gameId': '1663',
'tagAll': '0',
'page': page_num
}
time.sleep(randint(1,5))
img_lists = getData(url, params, headers)
img_content = get_img_content(img_lists)
saveData(img_lists, img_content)
def getData(url, params, headers):
json_text = requests.get(url=url, params=params, headers=headers).text
jsonObj = json.loads(json_text) #解码的过程,把乱码变成了中文
datas = jsonObj['data']['datas']
#print(datas)
img_lists = []
for data in datas:
img = []
nick = data['nick']
screenshot = data['screenshot']
img.append(nick)
img.append(screenshot)
img_lists.append(img)
return img_lists
def get_img_content(img_lists):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
img_content = []
for i in range(len(img_lists)):
src = img_lists[i][1]
content = requests.get(url=src, headers=headers).content
print('解析完毕第{}张图片!'.format(i))
img_content.append(content)
return img_content
def saveData(img_lists, img_content):
if not os.path.exists('./xingxiu'):
os.mkdir('./xingxiu')
for i in range(len(img_lists)):
img_name = img_lists[i][0] + '.jpg'
img_path = './xingxiu/' + img_name
with open(img_path, 'wb') as fp:
fp.write(img_content[i])
if __name__ == '__main__':
main()
标签:小姐姐,img,headers,url,lists,content,虎牙,params,合集 来源: https://www.cnblogs.com/python-code/p/14727998.html