我想为我的世界服务器做一个解析器,所以我开始做它,这是一个死胡同。它显示相同的ip版本等等,我需要一切都不同,我的代码是:
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 ']}
您的错误是您将 ip、版本等标签 1 次(遇到的第一个)并立即“将其投入战斗”。解决问题的代码: