Попытка очистить Wikitables с помощью Pandas и BeautifulSoupPython

Программы на Python
Anonymous
Попытка очистить Wikitables с помощью Pandas и BeautifulSoup

Сообщение Anonymous »

Здравствуйте, я пытаюсь очистить таблицы данных с помощью Pandas и BeautifulSoup, но сталкиваюсь с ошибкой. Когда я пытаюсь извлечь данные из таблицы с именем из двух слов, ошибка говорит о том, что она не может объединить фрейм данных, потому что нет значений в моем массиве DF
Эта функция работает

from bs4 import BeautifulSoup
from io import StringIO
import requests
import pandas as pd
import numpy as np

def getRandomGun():
weaponWikiLink = 'https://escapefromtarkov.fandom.com/wiki/Weapons'

weaponSoupA = BeautifulSoup(requests.get(weaponWikiLink).content, 'html.parser')

weaponDF = []

for i in weaponSoupA.select(".wikitable"):
for hr in i.select("hr"):
hr.replace_with(", ")
df = pd.read_html(StringIO(str(i)))[0]
title = i.find_previous("h3").span.text
df["weapon_type"] = title
weaponDF.append(df)

df_out = pd.concat(weaponDF)

big_df = pd.concat([df_out[df_out['weapon_type'].isin(["Shotguns", "Submachine guns",
"Light machine guns", "Assault carbines",
"Assault rifles", "Designated marksman rifles",
"Sniper rifles", "Pistols"])]])
big_df.reset_index(drop=True, inplace=True)

randGun = big_df.sample(n=1)

print (randGun[["Name"]])

пока эта функция возвращает ошибку
def getRandChestRig():
chestRigWikiLink = 'https://escapefromtarkov.fandom.com/wiki/Chest_vests'
chestRigSoupA = BeautifulSoup(requests.get(chestRigWikiLink).content, 'html.parser')

chestRigDF = []

for i in chestRigSoupA.select(".wikitable"):
for hr in i.select("hr"):
hr.replace_with(", ")
df = pd.read_html(StringIO(str(i)))[0]
title = i.find_previous("h2").span.text
df["chest_rigs_type"] = title
chestRigDF.append(df)

df_out = pd.concat(chestRigDF)

big_df = pd.concat([df_out[df_out['chest_rigs_type'].isin(["Unarmored", "Armored"])]])
big_df.reset_index(drop=True, inplace=True)

randChestRig = big_df.sample(n=1)

print (randChestRig[["Name"]])


Подробнее здесь: https://stackoverflow.com/questions/784 ... utifulsoup

Вернуться в «Python»