import requests
from bs4 import BeautifulSoup
import pymongo# 发起请求获取数据
res = requests.get("https://www.4399.com/")
# 解码数据
content = res.content.decode("gbk")
# 解析数据
soup = BeautifulSoup(content, parser='html.parser')# 获取推荐列表
game_list = soup.find("ul", attrs={"id": "fla_game"})
games = game_list.find_all("li")
game_datas = []
for game in games:game_data = {}# 解析每一个游戏soup_game = BeautifulSoup(str(game))# 获取图标game_data["img"] = soup_game.find("img").attrs['lz_src']info = soup_game.find("p")title_desc = info.findAll("a")# 获取标题game_data["title"] = title_desc[0].text# 获取描述game_data["dex"] = title_desc[1].textgame_datas.append(game_data)# 将数据列表插入数据库
client = pymongo.MongoClient()
db = client.get_database("4399今日推荐")
collect = db.get_collection("推荐列表")
collect.insert_many(game_datas)
client.close()