当前位置: 首页 > news >正文

世界上做的最好的前端网站7个湖北seo网站推广策略

世界上做的最好的前端网站,7个湖北seo网站推广策略,网页版梦幻西游宠物,wordpress子标题文章目录 关于坦克世界1. 爬虫任务2. 获取坦克列表3. 获取坦克具体信息结束语 关于坦克世界 《坦克世界》(World of Tanks, WOT)是我在本科期间玩过的一款战争网游,由Wargaming公司研发。2010年10月30日在俄罗斯首发,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/#wot&w_m=tanks

在这里插入图片描述

1. 爬虫任务

在这里插入图片描述
  当前的WOT有五种坦克类型,11个系别。我们要构建一个关于坦克百科的知识图谱,接下来就要通过爬虫来获取所有坦克的详细信息,比如坦克的等级、火力、机动性、防护能力、侦察能力等等。以当前的八级霸主中国重型坦克BZ-176为例,坦克的详细信息如下:

在这里插入图片描述
在这里插入图片描述

2. 获取坦克列表

在这里插入图片描述
  常规操作,F12+F5查看一下页面信息,定位到坦克列表的具体请求:

在这里插入图片描述
  是一个POST请求,返回的是一个JSON格式的数据,包含了该类型坦克的一些基本信息:

在这里插入图片描述
  POST请求参数如下:

在这里插入图片描述

  特别说明一下:构建该请求header时,Content-Length参数是必须的。

  代码实现:

# -*- coding: utf-8 -*-
# Author  : xiayouran
# Email   : youran.xia@foxmail.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;q=0.9',}self.post_headers = {'Accept': 'application/json, text/javascript, */*; q=0.01','Content-Length': '135','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-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, method='POST', from_data=self.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坦克的id60209,每个坦克都是唯一的,这个参数通过上一个步骤的POST请求可以获取到;
  Part 3:坦克的名称:Ch47_BZ_176,这个参数也可以通过上一个步骤的POST请求可以获取到。
  这样就可以为每个坦克构造一个对应的url了,只需解析该url对应的界面即可。解析的时候我分成了两部分,先对坦克的基本信息进行解析,比如坦克系别、等级及价格等等,由BeautifulSoup库实现,坦克的具体信息,比如火力、机动、防护及侦察能力,这些信息是由JavaScript代码动态请求得到的,这里为了简便没有分析具体的js代码,而是先使用selenium库进行网页渲染,然后再使用BeautifulSoup库进行解析。这里不再细说,下面给出页面解析的代码:

# -*- coding: utf-8 -*-
# Author  : xiayouran
# Email   : youran.xia@foxmail.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(name='h1', attrs={'class': 'garage_title garage_title__inline js-tank-title'}).strip()tank_statistic = soup.find_all(name='div', 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(name='p', attrs='garage_objection')tank_property2 = soup.find(name='p', attrs='garage_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(name='p', attrs='tank-description_notification')if tank_desc_tag:tank_info['历史背景'] = tank_desc_tag.texttank_parameter = soup.find_all(name='div', attrs={'class': 'specification_block'})for tp_tag in tank_parameter:param_text = tp_tag.find_next(name='h2', attrs={'class': 'specification_title specification_title__sub'}).get_text()# spec_param = tp_tag.find_all_next(name='div', 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(), desc='Crawling'):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, method='GET')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等资料^_^

http://www.hkea.cn/news/278713/

相关文章:

  • 做mla的网站网站优化哪家好
  • 网站注册的账号怎么注销线上营销活动有哪些
  • 国内做进口的电商网站网站推广软件哪个好
  • 谁有做那事的网站百度投诉中心入口
  • 免费单页网站在线制作沈阳seo排名优化教程
  • 廊坊网站建大型网站建站公司
  • 远程桌面做网站sem和seo区别与联系
  • 做贷款网站优化大师有用吗
  • 有没有便宜的网站制作制作网页教程
  • 医院网站制作优化关键词的方法有哪些
  • wordpress安装到网站吗泰安seo
  • 长春网站开发培训价格google play三件套
  • 做生存分析的网站有哪些国外新闻最新消息
  • 济南网站优化收费百度互联网营销
  • bootstrap响应网站模板下载发帖推广百度首页
  • 动态网站上的查询怎么做新媒体运营培训学校
  • 网站开发人员必备技能百度优化推广
  • 花都 网站建设百度推广怎么添加关键词
  • 开发公司成本部职责岗位职责和流程苏州网站建设优化
  • 湛江网站制作系统seo排名需要多少钱
  • 城乡现代社区建设seo关键词推广案例
  • 旅游网站开发外文文献关键洞察力
  • 大学生asp网站开发的实训周长沙百度快速优化
  • 黑龙江省建设网站百度投流运营
  • 网站关键词太多好不好兰州seo整站优化服务商
  • 义乌网站设计网店推广策划方案
  • 无锡网站优化工作室网站关键词排名优化推广软件
  • 长沙做网站的公司亚马逊seo什么意思
  • 仪征建设银行官方网站怎么优化一个网站
  • 那个网站可以查询美做空基金宁波网站推广平台效果好