Проблема с парсером серверов майнкрафт
Я хочу сделать парсер серверов майнкрафт, вот начал делать и тут тупик. Он отображает один и тот же айпи версию и тд, мне нужно чтобы было все разное, мой код:
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
URL_TEMPLATE = "https://monitoringminecraft.ru/novie-servera"
FILE_NAME = "test.csv"
def parse():
result_list = {'href': [], 'title': [], 'about': []}
r = requests.get(URL_TEMPLATE)
soup = bs(r.text, "html.parser")
server = soup.find('tr', class_='server')
ru = soup.find_all('div', class_='flag ru')
ip = soup.find('span', class_='ip_serv')
version = soup.find('td', class_='ver')
opened = soup.find('td', class_='opened')
online = soup.find('div', class_='wrap')
clear_ip = [c.text for c in ip]
clear_version = [c.text for c in version]
clear_opened = [c.text for c in opened]
clear_online = [c.text for c in online]
comps = []
for servers in server:
comps.append({
'ip': clear_ip,
'version': clear_version,
'opened': clear_opened,
'online': clear_online
})
for comp in comps:
print(comp)
parse()
что выдается в консоли:
{'ip': ['89.239.167.113:19132'], 'version': ['?'], 'opened': ['только что'], 'online': [' ', '0', ' из 2022 ']}
{'ip': ['89.239.167.113:19132'], 'version': ['?'], 'opened': ['только что'], 'online': [' ', '0', ' из 2022 ']}
{'ip': ['89.239.167.113:19132'], 'version': ['?'], 'opened': ['только что'], 'online': [' ', '0', ' из 2022 ']}
{'ip': ['89.239.167.113:19132'], 'version': ['?'], 'opened': ['только что'], 'online': [' ', '0', ' из 2022 ']}
Ответы (1 шт):
Автор решения: Prog
→ Ссылка
Ваша ошибка была в том, что вы 1 раз брали тег ip, version и т.д.(первый попавшийся) и сразу "кидали в бой". Код с устранением проблемы:
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
URL_TEMPLATE = "https://monitoringminecraft.ru/novie-servera"
FILE_NAME = "test.csv"
r = requests.get(URL_TEMPLATE)
soup = bs(r.text, "html.parser")
server = []
server = soup.find_all('tr', class_='server')
sym_ = len(server) - 1
amount = 0
_sym = 0
def parse(amount, _sym, sym_):
result_list = {'href': [], 'title': [], 'about': []}
r = requests.get(URL_TEMPLATE)
soup = bs(r.text, "html.parser")
ru = soup.find_all('div', class_='flag ru')
ip = []
ip += soup.find_all('span', class_='ip_serv')
ip = ip[_sym:-sym_]
version = []
version = soup.find_all('td', class_='ver')
version = version[_sym:-sym_]
opened = []
opened += soup.find_all('td', class_='opened')
opened = opened[_sym:-sym_]
online = []
online += soup.find_all('div', class_='wrap')
online = online[_sym:-sym_]
clear_ip = [c.text for c in ip]
clear_version = [c.text for c in version]
clear_opened = [c.text for c in opened]
clear_online = [c.text for c in online]
comps = []
comps.append({
'ip': clear_ip,
'version': clear_version,
'opened': clear_opened,
'online': clear_online
})
print(comps)
if amount >= len(server)-1:
pass
else:
_sym+=1
sym_-=1
amount += 1
parse(amount, _sym, sym_)
parse(amount, _sym, sym_)