【发布时间】:2021-12-27 22:01:02
【问题描述】:
我想查看某个网站以从中收集数据,在第一次访问该网站时,我会收集所有数据以忽略它。如果添加了新行(例如此处的打印),我想执行某个操作。但是,每当出现新项目时,它似乎会打印网站上的每一行,即使我正在检查该行是否已经存在于字典中。不知道怎么解决,谁能看看?
import requests
import re
import copy
import time
from datetime import date
from bs4 import BeautifulSoup
class KillStatistics:
def __init__(self):
self.records = {}
self.watched_names = ["Test"]
self.iter = 0
def parse_records(self):
r = requests.get("http://149.56.28.71/?subtopic=killstatistics")
soup = BeautifulSoup(r.content, "html.parser")
table = soup.findChildren("table")
for record in table:
for data in record:
if data.text == "Last Deaths":
pass
else:
entry = data.text
entry = re.split("..?(?=[0-9][A-Z]).", data.text)
entry[0] = entry[0].split(", ")
entry[0][0] = entry[0][0].split(".")
entry_id, day, month, year, hour = (
entry[0][0][0],
entry[0][0][1],
entry[0][0][2],
entry[0][0][3],
entry[0][1],
)
message = entry[1]
nickname = (re.findall(".+?(?=at)", message)[0]).strip()
killed_by = (re.findall(r"(?<=\bby).*", message)[0]).strip()
if self.iter < 1:
"""Its the first visit to the website, i want it to download all the data and store it in dictionary"""
self.records[
entry_id
] = f"{nickname} was killed by {killed_by} at {day}-{month}-{year} {hour}"
elif (
self.iter > 1
and f"{nickname} was killed by {killed_by} at {day}-{month}-{year} {hour}"
not in self.records.values()
):
"""Here I want to look into the dictionary to check if the element exists in it,
if not print it and add to the dictionary at [entry_id] so we can skip it in next iteration
Don't know why but whenever a new item appears on the website it seems to edit every item in the dictionary instead of just editing the one
that wasnt there"""
print(
f"{nickname} was killed by {killed_by} at {day}-{month}-{year} {hour}"
)
self.records[
entry_id
] = f"{nickname} was killed by {killed_by} at {day}-{month}-{year} {hour}"
print("---")
self.iter += 1
ks = KillStatistics()
if __name__ == "__main__":
while True:
ks.parse_records()
time.sleep(10)
entry_id 始终与其 500 行数据相同,并且它们的 id 为 1,2,3...500,最新的始终为 1。我知道我总是可以检查 1 以获得最新的,但有时例如10 个玩家可以同时死亡,所以我想检查他们是否有变化,并且只在新玩家上执行打印。
当前输出:
Velerion was killed by Rat and Cave Rat at 27-12-2021 16:53
Scrappy was killed by Cursed Queen at 27-12-2021 16:52
Velerion was killed by Rat at 27-12-2021 16:28
Velerion was killed by Rat at 27-12-2021 16:22
Velerion was killed by Rat at 27-12-2021 16:21
Velerion was killed by Rat at 27-12-2021 15:51
Shade was killed by Tentacle Slayer at 27-12-2021 15:46
Mr Yahoo was killed by Immortal Hunter at 27-12-2021 15:41
Scrappy was killed by Witch Hunter at 27-12-2021 15:39
Barbudo Arqueiro was killed by Seahorse at 27-12-2021 15:23
Emperor Martino was killed by Dark Slayer at 27-12-2021 15:14
Shade was killed by Tentacle Slayer at 27-12-2021 15:11
Head Hunter was killed by Demon Blood Slayer at 27-12-2021 15:09
预期输出:
Velerion was killed by Rat and Cave Rat at 27-12-2021 16:53
【问题讨论】:
-
entry_id是您的密钥。你应该搜索那个,而不是那个长字符串。 -
我现在尝试做类似的事情
elif (self.iter > 1and self.records[entry_id] != f"{nickname} was killed by {killed_by} at {day}-{month}-{year} {hour}" ):,但它的行为完全相同。 entry_id 始终与其 500 行数据相同,并且它们的 id 为 1,2,3...500,最新的始终为 1。我知道我总是可以检查 1 以获得最新的,但有时例如 10 个玩家可以同时死,所以我想检查它们是否有变化,并且只在新的上执行打印 -
那么你不能使用
entry_id作为你的字典键。您需要跟踪最新条目的时间戳,并在到达该时间戳时停止查找。我会在下面告诉你。
标签: python python-3.x beautifulsoup python-requests