Codekratzer Eine Datentabelle < /li>
Postdatenmanipulation In jeder zweiten Textzeile enthält eine Header < /li>
Die ungeraden Zeilen enthalten eine Liste von Karten, die ein Verschluss sind. Ein Regexp (so würden diese für so viele Zeilen aufgeteilt, wie es abgrenzte Einträge gibt) < /li>
Die erste Zeile würde in Bezug auf Zeichen zu kurz ausgeschlossen (versucht, aber versäumt es nicht vollständig zu entfernen leere Zeichenfolgen) < /li>
< /ol>
Ich bin festgefahren, wie man die Header und Daten in mein 2D -Array schreibt. Tia < /p>
Code: Select all
import requests
import regex
from bs4 import BeautifulSoup
url = 'https://modernarmor.worldoftanks.com/en/cms/guides/map-labels-tiers-eras/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
matrix = [[]]
table = soup.find('table')
table_body = table.find('tbody')
rows = table.find_all('tr')
for row in rows:
cols = row.find_all(['td', 'th'])
coldata = [ele.text.strip() for ele in cols]
coldata = str(coldata).replace('[MERGE]',"")
# failed attempt to remove empty strings from each line
#coldata = filter(None, coldata)
x = regex.sub(r'([a-z]{1})([A-Z]{1})', r'\1,\2', coldata).strip()
lst = x.split(",")
stringCount = len(lst)
print (stringCount)
# would be cleaner to remove empty strings and write odd lines as headers, even lenes as data
if stringCount == 2:
print ("header"+x)
# add headers here
elif stringCount> 2:
print (lst)
#list data here
else:
print ("failed to remove single empty string")
#data.append([ele for ele in coldata if ele])
#print(data)