抓取L4d2地图信息

2024-04-17 22:58
文章标签 抓取 地图信息 l4d2

本文主要是介绍抓取L4d2地图信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

程序用途:从 www.orangetage.com/map/ 获取指定页数的所有地图的信息 储存到txt文件
效果演示

# -*- coding: utf-8 -*-
# Version: Python 3.9.5
# Author: TRIX
# Date: 2021-09-07 21:22:40
# Use: 从http://www.orangetage.com/map/获取指定页数的所有地图的信息 储存到txt文件
from logging import basicConfig,DEBUG,debug,CRITICAL,disable # Import debugging module
#disable(CRITICAL)# Remove # when the program is completed
basicConfig(level=DEBUG, format='%(levelname)s: %(message)s. [%(lineno)d]%(filename)s <%(asctime)s>',filename='debug.log',filemode='w')# Set debugging mode. Replace print() with debug()import 	requests,bs4
def get_html(page):#获取每页的htmlpages_list=[]#判断页数类型if '-' in page:#如果页数类似 12-32pages_list.extend(page.split('-'))for n in range(int(pages_list[0])+1,int(pages_list[-1])):pages_list.insert(1,n)else:pages_list.append(page)#如果页数类似 15#页数转页面源代码#如果第一页是1 且只有一页if pages_list[0]=='1' and len(pages_list)==1:pages_list[0]=requests.get('http://www.orangetage.com/map/index.html')#如果第一页是1 且不只一页elif pages_list[0]=='1' and len(pages_list)!=1:pages_list[0]=requests.get('http://www.orangetage.com/map/index.html')for i,e in enumerate(pages_list[1:],1):pages_list[i]=requests.get('http://www.orangetage.com/map/{}.html'.format(e))#如果第一页不是1 且只有一页elif pages_list[0]!='1' and len(pages_list)==1:pages_list[0]=requests.get('http://www.orangetage.com/map/{}.html'.format(pages_list[0]))#如果第一页不是1 且不只一页elif pages_list[0]!='1' and len(pages_list)!=1:for i,e in enumerate(pages_list,0):pages_list[i]=requests.get('http://www.orangetage.com/map/{}.html'.format(e))#设置编码格式for i,n in enumerate(pages_list):#print(pages_list[i].apparent_encoding)#网页编码格式 得知是gbkpages_list[i].encoding='gbk'#设置编码为gbkpages_list[i]=pages_list[i].textreturn pages_listdef get_map(page):#寻找地图pages_list=get_html(page)#debug(len(pages_list))#记录调试日志map_url_list=[]#寻找页面里的地图urlfor i,n in enumerate(pages_list):pages_list[i]=bs4.BeautifulSoup(n,'lxml').select('div[class="list_img"] > a')#处理html 寻找 <div class="list_img"> 里的 <a>for x,url_tag in enumerate(pages_list[i]):map_url_list.append(url_tag.get('href'))#寻找 <a> 里的 url#debug(pages_list)page_count=1map_count=1map_list=['' for map_i,n in enumerate(map_url_list)]#地图所有相关信息 组成列表#寻找地图页面里的地图信息 和 下载地址for map_i,n in enumerate(map_url_list):map_url_list[map_i]=requests.get(n)map_url_list[map_i].encoding='gbk'map_url_list[map_i]=map_url_list[map_i].text#获得地图htmlmap_list[map_i]+='------第{}页-第{}个地图------\n'.format(page_count,map_count)map_count+=1if map_count==9:map_count=1page_count+=1#获取地图信息map_info_list=bs4.BeautifulSoup(map_url_list[map_i],'lxml').select('span[style="font-family:微软雅黑;"]')#寻找 <span style="font-family:微软雅黑;"> 里的地图信息if len(map_info_list)!=1:map_list[map_i]+='地图简介:'for info_i,info_tag in enumerate(map_info_list):map_info_list[info_i]=info_tag.text.replace('\r\n','\n')#处理地图信息map_list[map_i]+=map_info_list[info_i]+'\n'#获取地图下载地址map_link_list=bs4.BeautifulSoup(map_url_list[map_i],'lxml').select('ul[class="l xz_a wrap blue"] > li > a')#寻找 <ul class="l xz_a wrap blue"> 里的 <li> 里的 <a>#debug(map_link_list)for link_i,url_tag in enumerate(map_link_list):map_list[map_i]+=url_tag.text+':'map_list[map_i]+=url_tag.get('href')+'\n'#添加下载地址到map_list对应元素map_list[map_i]+='\n\n'with open('l4d2_maps_info.txt','w',encoding='utf-8') as f:#写入txtf.write('在第{}页一共找到{}个地图\n\n\n'.format(page,len(map_url_list)))for map_i,n in enumerate(map_url_list):f.write(map_list[map_i])get_map('18-25')

这篇关于抓取L4d2地图信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/913092

相关文章

PHP抓取网站图片脚本

方法一: <?phpheader("Content-type:image/jpeg"); class download_image{function read_url($str) { $file=fopen($str,"r");$result = ''; while(!feof($file)) { $result.=fgets($file,9999); } fclose($file); re

用Java抓取CSDN主页上的图片

一,步骤一:获取网页源码 1,定义要爬取的页面的URL对象 //定义即将访问的链接String url="http://www.csdn.net";//获取CSDN的URL对象URL realURL = new URL(url); 2,获得这个链接的一个连接对象 URLConnection connection = realURL.openConnection();

【go语言爬虫】go语言高性能抓取手机号码归属地、所属运营商

一、需求分析 根据手机号码获取手机号码的归属地和所属运营商类型 类似:四川 18683339513 乐山 614000 0833 中国联通 二、运行效果 三、实现源代码 package main//网址:https://github.com/M2shad0w/phone-go//安装包:go get github.com/M2shad0w/phone-goimport ("fmt

【python爬虫】网贷天眼平台表格数据抓取

一、需求分析 抓取url: http://www.p2peye.com/shuju/ptsj/ 抓取字段: 昨日数据 排序 平台名称 成交额 综合利率 投资人 借款周期 借款人 满标速度 累计贷款余额 资金净流入 二、python爬虫源代码 # -*- coding:utf-8*-import sysreload(sys)sys.setdefaultencoding('utf-

【R语言爬虫】网贷天眼数据平台表格数据抓取2

一、需求分析 抓取url: http://www.p2peye.com/shuju/ptsj/ 昨日数据: 字段:排序 平台名称 成交额 综合利率 投资人 借款周期 借款人 满标速度 累计贷款余额 资金净流入 二、rvest爬虫实现源代码 rm(list=ls())gc()options(scipen = 200)library('rvest')timestart<-Sys

【python 百度指数抓取】python 模拟登陆百度指数,图像识别百度指数

一、算法思想 目的奔着去抓取百度指数的搜索指数,搜索指数的爬虫不像是其他爬虫,难度系数很高,分析之后发现是图片,坑爹的狠,想了下,由于之前做过身份证号码识别,验证码识别之类,豁然开朗,不就是图像识别麽,图像识别我不怕你,于是就有了思路,果然有异曲同工之妙,最后成功被我攻破了,大致思路如下: 1、首先得模拟登陆百度账号(用selenium+PhantomJS模拟登陆百度,获取cookie) 2

python scrapy爬虫框架 抓取BOSS直聘平台 数据可视化统计分析

使用python scrapy实现BOSS直聘数据抓取分析 前言   随着金秋九月的悄然而至,我们迎来了业界俗称的“金九银十”跳槽黄金季,周围的朋友圈中弥漫着探索新机遇的热烈氛围。然而,作为深耕技术领域的程序员群体,我们往往沉浸在代码的浩瀚宇宙中,享受着解决技术难题的乐趣,却也不经意间与职场外部的风云变幻保持了一定的距离,对行业动态或许仅有一鳞半爪的了解,甚至偶有盲区。   但正是这份对技术

Scrapy ——自动多网页爬取(抓取某人博客所有文章)(四)

首先创建project:   [python]  view plain  copy     转存失败重新上传取消<

python爬虫: 抓取任意歌手的歌词,简直不要太骚

估计大家对歌词的抓取一般是通过抓取网页内容的方式来进行,今天,LZ就教大家一个简单的方法。对大家进行歌词分析来说,又多了一条捷径。 本篇文章是通过请求qq音乐的某一个文件来进行获取的,这个骚操作恐怕还没什么人发现吧,娃哈哈~ 看完过后你就会觉得,这简直不要太骚~ 二话不说, 先上代码: #!/usr/bin/python# -*- coding:utf-8 -*-import reque

网络数据抓取技术栈详解:从传统到现代的全面指南

在大数据和信息化时代,网络数据抓取已经成为获取数据的一个关键手段。Python 作为一门灵活且功能强大的语言,拥有丰富的库和框架来支持各种网络抓取需求。本文将为你详细介绍几种主流的 Python 抓取工具,从传统的静态网页抓取到现代的动态内容处理,希望能帮助你选择适合自己项目的最佳方案。 1. Requests Requests 是 Python 最流行的 HTTP 库之一,用于发送 HTTP