1 Star 0 Fork 0

Kazin / GameCrawler_4399

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
4399GameCrawler.py 1.91 KB
一键复制 编辑 原始数据 按行查看 历史
Kazin 提交于 2020-04-21 23:20 . init
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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kazin/GameCrawler_4399.git
git@gitee.com:kazin/GameCrawler_4399.git
kazin
GameCrawler_4399
GameCrawler_4399
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891