抓取L4d2地圖信息

程序用途:從 www.orangetage.com/map/ 獲取指定頁數(shù)的所有地圖的信息 儲(chǔ)存到txt文件
效果演示

# -*- coding: utf-8 -*-
# Version: Python 3.9.5
# Author: TRIX
# Date: 2021-09-07 21:22:40
# Use: 從http://www.orangetage.com/map/獲取指定頁數(shù)的所有地圖的信息 儲(chǔ)存到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):#獲取每頁的html
    pages_list=[]
    #判斷頁數(shù)類型
    if '-' in page:#如果頁數(shù)類似 12-32
        pages_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)#如果頁數(shù)類似 15

    #頁數(shù)轉(zhuǎn)頁面源代碼
    #如果第一頁是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))

    #設(shè)置編碼格式
    for i,n in enumerate(pages_list):
        #print(pages_list[i].apparent_encoding)#網(wǎng)頁編碼格式 得知是gbk
        pages_list[i].encoding='gbk'#設(shè)置編碼為gbk
        pages_list[i]=pages_list[i].text

    return pages_list

def get_map(page):#尋找地圖
    pages_list=get_html(page)
    #debug(len(pages_list))#記錄調(diào)試日志
    map_url_list=[]
    #尋找頁面里的地圖url
    for 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=1
    map_count=1

    map_list=['' for map_i,n in enumerate(map_url_list)]#地圖所有相關(guān)信息 組成列表
    #尋找地圖頁面里的地圖信息 和 下載地址
    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#獲得地圖html
        map_list[map_i]+='------第{}頁-第{}個(gè)地圖------\n'.format(page_count,map_count)
        map_count+=1
        if map_count==9:
            map_count=1
            page_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]+='地圖簡(jiǎn)介:'
        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對(duì)應(yīng)元素
        map_list[map_i]+='\n\n'

    with open('l4d2_maps_info.txt','w',encoding='utf-8') as f:#寫入txt
        f.write('在第{}頁一共找到{}個(gè)地圖\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')

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容