网站建设售后服务承诺书,推广普通话文字内容,淮安网站推广,wordpress query_posts参数文章目录 关于坦克世界1. 爬虫任务2. 获取坦克列表3. 获取坦克具体信息结束语 关于坦克世界 《坦克世界》(World of Tanks, WOT)是我在本科期间玩过的一款战争网游#xff0c;由Wargaming公司研发。2010年10月30日在俄罗斯首发#xff0c;2011年4月12日在北美和欧洲推出… 文章目录 关于坦克世界1. 爬虫任务2. 获取坦克列表3. 获取坦克具体信息结束语 关于坦克世界 《坦克世界》(World of Tanks, WOT)是我在本科期间玩过的一款战争网游由Wargaming公司研发。2010年10月30日在俄罗斯首发2011年4月12日在北美和欧洲推出2011年3月15日在中国由空中网代理推出2020年国服由360代理。游戏背景设定在二战时期玩家会扮演1930到1960年代的战车进行对战要求战略和合作性游戏中的战车根据历史高度还原。 坦克世界官网https://wotgame.cn/ 坦克世界坦克百科https://wotgame.cn/zh-cn/tankopedia/#wotw_mtanks 1. 爬虫任务 当前的WOT有五种坦克类型11个系别。我们要构建一个关于坦克百科的知识图谱接下来就要通过爬虫来获取所有坦克的详细信息比如坦克的等级、火力、机动性、防护能力、侦察能力等等。以当前的八级霸主中国重型坦克BZ-176为例坦克的详细信息如下 2. 获取坦克列表 常规操作F12F5查看一下页面信息定位到坦克列表的具体请求 是一个POST请求返回的是一个JSON格式的数据包含了该类型坦克的一些基本信息 POST请求参数如下 特别说明一下构建该请求header时Content-Length参数是必须的。 代码实现
# -*- coding: utf-8 -*-
# Author : xiayouran
# Email : youran.xiafoxmail.com
# Datetime: 2023/9/29 22:43
# Filename: spider_wot.py
import os
import time
import json
import requestsclass WOTSpider:def __init__(self):self.base_headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36,Accept-Encoding: gzip, deflate, br,Accept-Language: zh-CN,zh;q0.9,}self.post_headers {Accept: application/json, text/javascript, */*; q0.01,Content-Length: 135,Content-Type: application/x-www-form-urlencoded; charsetUTF-8}self.from_data {filter[nation]: ,filter[type]: lightTank,filter[role]: ,filter[tier]: ,filter[language]: zh-cn,filter[premium]: 0,1}self.tank_list_url https://wotgame.cn/wotpbe/tankopedia/api/vehicles/by_filters/self.tank_label [lightTank, mediumTank, heavyTank, AT-SPG, SPG]self.tanks {}def parser_tanklist_html(self, html_text):json_data json.loads(html_text)for data in json_data[data][data]:self.tanks[data[0] _ data[4]] {tank_nation: data[0],tank_type: data[1],tank_rank: data[3],tank_name: data[4],tank_name_s: data[5],tank_url: data[6],tank_id: data[7]}def run(self):for label in self.tank_label:self.from_data[filter[type]] labelhtml_text self.get_html(self.tank_list_url, methodPOST, from_dataself.from_data)if not html_text:print([{}] error.format(label))continueself.parser_tanklist_html(html_text)time.sleep(3)self.save_json(os.path.join(self.data_path, tank_list.json), self.tanks)if __name__ __main__:tank_spider WOTSpider()tank_spider.run()上述代码只实现了一些重要的函数及变量声明完整的代码可以从github上拉取WOT
3. 获取坦克具体信息 坦克具体信息的页面就是一个纯HTML页面了一个GET请求就可以获得。当然啦具体怎么分析的就不细说了对爬虫技术感兴趣的同学们可以找找资料这里就只说一下抓取流程。 先分析GET请求https://wotgame.cn/zh-cn/tankopedia/60209-Ch47_BZ_176/可以分成三部分 Part 1基本的url请求https://wotgame.cn/zh-cn/tankopedia Part 2坦克的idBZ-176坦克的id为60209每个坦克都是唯一的这个参数通过上一个步骤的POST请求可以获取到 Part 3坦克的名称Ch47_BZ_176这个参数也可以通过上一个步骤的POST请求可以获取到。 这样就可以为每个坦克构造一个对应的url了只需解析该url对应的界面即可。解析的时候我分成了两部分先对坦克的基本信息进行解析比如坦克系别、等级及价格等等由BeautifulSoup库实现坦克的具体信息比如火力、机动、防护及侦察能力这些信息是由JavaScript代码动态请求得到的这里为了简便没有分析具体的js代码而是先使用selenium库进行网页渲染然后再使用BeautifulSoup库进行解析。这里不再细说下面给出页面解析的代码
# -*- coding: utf-8 -*-
# Author : xiayouran
# Email : youran.xiafoxmail.com
# Datetime: 2023/9/29 22:43
# Filename: spider_wot.py
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup, Tag
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWaitclass WOTSpider:def __init__(self):passdef is_span_with_value(self, driver):try:element driver.find_element(By.XPATH, //span[data-bind\text: ttc().getFormattedBestParam(maxHealth, gt)\])data element.text.strip()if data:return Trueexcept:return Falsedef get_html_driver(self, url):self.driver.get(url)self.wait.until(self.is_span_with_value)page_source self.driver.page_sourcereturn page_sourcedef parser_tankinfo_html(self, html_text):tank_info copy.deepcopy(self.tank_info)soup BeautifulSoup(html_text, lxml)# tank_name soup.find(nameh1, attrs{class: garage_title garage_title__inline js-tank-title}).strip()tank_statistic soup.find_all(namediv, attrs{class: tank-statistic_item})for ts in tank_statistic:ts_text [t for t in ts.get_text().split(\n) if t]if len(ts_text) 5:tank_info[价格] {银币: ts_text[-3],经验: ts_text[-1]}else:tank_info[ts_text[0]] ts_text[-1]tank_property1 soup.find(namep, attrsgarage_objection)tank_property2 soup.find(namep, attrsgarage_objection garage_objection__collector)if tank_property1:tank_info[性质] tank_property1.textelif tank_property2:tank_info[性质] tank_property2.textelse:tank_info[性质] 银币坦克tank_desc_tag soup.find(namep, attrstank-description_notification)if tank_desc_tag:tank_info[历史背景] tank_desc_tag.texttank_parameter soup.find_all(namediv, attrs{class: specification_block})for tp_tag in tank_parameter:param_text tp_tag.find_next(nameh2, attrs{class: specification_title specification_title__sub}).get_text()# spec_param tp_tag.find_all_next(namediv, attrs{class: specification_item})spec_param [tag for tag in tp_tag.contents if isinstance(tag, Tag) and tag.attrs[class] [specification_item]]spec_info {}for tp in spec_param:tp_text [t for t in tp.get_text().replace( , ).split(\n) if t]if not tp_text or not tp_text[0][0].isdigit():continuespec_info[tp_text[-1]] .join(tp_text[:-1])tank_info[param_text] spec_inforeturn tank_infodef run(self):file_list [os.path.basename(file)[:-5] for file in glob.glob(os.path.join(self.data_path, *.json))]for k, item in tqdm(self.tanks.items(), descCrawling):file_name k.replace(, ).replace(“, ).replace(”, ).replace(/, -).replace(\\, ).replace(*, )if file_name in file_list:continuetank_url self.tank_url str(item[tank_id]) - item[tank_url]html_text self.get_html_driver(tank_url)# html_text self.get_html(tank_url, methodGET)tank_info self.parser_tankinfo_html(html_text)self.tanks[k].update(tank_info)self.save_json(os.path.join(self.data_path, {}.json.format(file_name)), self.tanks[k])time.sleep(1.5)self.save_json(os.path.join(self.data_path, tank_list_detail.json), self.tanks)if __name__ __main__:tank_spider WOTSpider()tank_spider.run()大约半个小时即可获取全部的坦克信息如下 Selenium 库依赖chromedriver需要根据自己的Chrome浏览器版本下载合适的版本chromedriver的官方下载地址为https://chromedriver.chromium.org/downloads/version-selection 结束语 本篇的完整代码及爬取的结果已经同步到仓库中感兴趣的话可以拉取一下下一篇文章就基于当前获取到的坦克信息来构造一个关于坦克百科的知识图谱。
开源代码仓库 如果喜欢的话记得给我的GitHub仓库WOT点个Star哦ヾ(≧∇≦*)ヾ 公众号已开通夏小悠关注以获取更多关于Python文章、AI领域最新技术、LLM大模型相关论文及内部PPT等资料^_^