我正试图从世界计量器中提取各国有关电晕病例的信息。 由于某些原因,我不能按类来针对特定的TR标记(python控制台中缺少这些标记上的类,但ChromeDeveloper中有)。所以我以所有tr元素为目标,然后过滤它们。一切都很好,但出于某种奇怪的原因,中国在前十名国家中被遗漏了。中国的html标签没有什么不同,但我还是不能把它放在那里。有什么想法吗? '''
r = requests.get("https://www.worldometers.info/coronavirus/")
content = r.content
soup = BeautifulSoup(content, "html.parser")
all_rows = soup.find_all("tr")
startingIndex = None
for index,each in enumerate(all_rows,start=0):
if "World" in each.text: # After that word "WORLD" comes TR elements of individual countries.
startingIndex = index
break
top10 = all_rows[startingIndex+1:startingIndex+11] # here i select top 10 countries that i need.
for index,each in enumerate(top10,start = 1):
droebiti_list = each.text.split("\n")
print(f"{index}){droebiti_list[1]} - {droebiti_list[6]}") # and printing info about recovered people
'''
页面源变量
content
的国家顺序与表中的不同(由于javascript脚本或其他原因,顺序可能会改变)因此,您可以收集所有数据并根据总案例对其重新排序
有趣的输出:
无法确保此代码正常工作(“我在错误的环境中进行此操作”),但要清除数据,此代码应正常工作:
您只需根据需要修改
ScrapedResult
相关问题 更多 >
编程相关推荐