代码拉取完成,页面将自动刷新
import os
import re
import threading
from bs4 import BeautifulSoup as bs
import requests
def getAllGameUrl():
"""
获取所有游戏的名称和游戏信息页的链接
:return:
"""
gameUrlList = []
response = requests.get('http://www.4399.com/flash/gamehw.htm')
dom = bs(response.content, 'html.parser')
gameLiList = dom.select('#skinbody > div:nth-child(6) > ul > li')
for i in gameLiList:
# 获取游戏的名称
gameName = i.select_one('a > b').get_text()
# 获取游戏信息页的链接
# 'http://www.4399.com/flash/212103.htm'
gameInfoUrl = indexUrl + i.select_one('a')['href']
gameUrlList.append({'gameName': gameName, 'gameInfoUrl': gameInfoUrl})
return gameUrlList
def downloadIfAvailable(game):
"""
判断一个游戏是否支持本地下载
:return:
"""
response = requests.get(game['gameInfoUrl'])
plainText = response.text
relativeUrlList = re.findall(r'(?<=_strGamePath=").+?\.swf', plainText)
if len(relativeUrlList) != 0:
gameUrl = gameBaseUrl + relativeUrlList[0]
game['gameUrl'] = gameUrl
threading.Thread(target=downloadAGame, args=(game,)).start()
def downloadAGame(game):
"""
根据下载链接下载游戏,并保存到.swf文件
:param game:
:return:
"""
downloadPath = 'games/'
if not os.path.exists(downloadPath):
try:
os.mkdir(downloadPath)
except FileExistsError as e:
print(e)
with open(downloadPath + game['gameName'] + '.swf', 'wb') as file:
file.write(requests.get(game['gameUrl']).content)
print(game['gameName'] + '下载完成')
if __name__ == '__main__':
indexUrl = 'http://www.4399.com'
gameBaseUrl = 'http://sxiao.4399.com/4399swf'
gameUrlList = getAllGameUrl()
for i in gameUrlList:
threading.Thread(target=downloadIfAvailable, args=(i,)).start()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。