Hello I am Trying To Scrape Data Tables With Pandas & BeautifulSoup But Running Into An Error When I try To Pull Data From A Table With A Two Word Name, The Error Is Saying That it cannot concatenate the dataframe because there are no values in my DF Array
This Function Works
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"]])
while this function returns the error
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"]])