python爬取游戏数据
时间: 2023-10-01 16:11:09 浏览: 97
爬取游戏数据可以使用 Python 中的 requests 库和 BeautifulSoup 库。
首先,你需要分析游戏数据所在的网站的 HTML 结构。然后使用 requests 库获取网页的 HTML 代码,再用 BeautifulSoup 库解析 HTML 代码,找到目标数据所在的标签和属性,最后将数据提取出来。
例如,以 Steam 平台上的游戏《绝地求生》为例,你可以使用以下代码来爬取游戏的评价数量和好评率:
```python
import requests
from bs4 import BeautifulSoup
url = "https://store.steampowered.com/app/578080/PLAYERUNKNOWNS_BATTLEGROUNDS/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 获取评价数量
review_count = soup.find("span", attrs={"class": "game_review_summary positive"}).find("span", attrs={"class": "responsive_hidden"}).get_text()
print("评价数量:", review_count)
# 获取好评率
positive_percent = soup.find("span", attrs={"class": "game_review_summary positive"}).find("span", attrs={"class": "game_review_summary_pos_perc"}).get_text()
negative_percent = soup.find("span", attrs={"class": "game_review_summary negative"}).find("span", attrs={"class": "game_review_summary_neg_perc"}).get_text()
print("好评率:", positive_percent + " / " + negative_percent)
```
这里使用了 requests 库获取网页的 HTML 代码,再用 BeautifulSoup 库解析 HTML 代码,找到评价数量和好评率所在的标签和属性,最后将数据提取出来并打印输出。