Konvertieren Sie Daten in DataFrame in Python
Mit Hilfe von @JaSON ist hier ein Code, mit dem ich die Daten in der Tabelle von lokalem HTML abrufen kann und der Code Selen verwendet
from selenium import webdriver
driver = webdriver.Chrome("C:/chromedriver.exe")
driver.get('file:///C:/Users/Future/Desktop/local.html')
counter = len(driver.find_elements_by_id("Section3"))
xpath = "//div[@id='Section3']/following-sibling::div[count(preceding-sibling::div[@id='Section3'])={0} and count(following-sibling::div[@id='Section3'])={1}]"
print(counter)
for i in range(counter):
print('\nRow #{} \n'.format(i + 1))
_xpath = xpath.format(i + 1, counter - (i + 1))
cells = driver.find_elements_by_xpath(_xpath)
for cell in cells:
value = cell.find_element_by_xpath(".//td").text
print(value)
Wie können diese Zeilen in eine gültige Tabelle konvertiert werden, die ich in eine CSV-Datei exportieren kann? Hier ist der lokale HTML-Linkhttps://pastebin.com/raw/hEq8K75C
** @Paul Brennan: Nachdem counter-1
ich versucht habe, den Zähler so zu bearbeiten, dass er 17 Zeilen enthält, um den Fehler von Zeile 18 vorübergehend zu überspringen, habe ich den Dateinamen.txt und hier ist eine Momentaufnahme der Ausgabe

Antworten
Ich habe Ihren Code geändert, um eine einfache Ausgabe durchzuführen. Dies ist nicht sehr pythonisch, da keine vektorisierte Erstellung des Datenrahmens verwendet wird, aber hier ist, wie es funktioniert. Richten Sie zuerst Pandas ein, zweitens richten Sie einen Datenrahmen ein (aber wir kennen die Spalten noch nicht) und richten Sie dann die Spalten beim ersten Durchgang ein (dies führt zu Problemen, wenn variable Spaltenlängen vorhanden sind. Geben Sie dann die Werte in den Datenrahmen ein
import pandas as pd
from selenium import webdriver
driver = webdriver.Chrome("C:/chromedriver.exe")
driver.get('file:///C:/Users/Future/Desktop/local.html')
counter = len(driver.find_elements_by_id("Section3"))
xpath = "//div[@id='Section3']/following-sibling::div[count(preceding-sibling::div[@id='Section3'])={0} and count(following-sibling::div[@id='Section3'])={1}]"
print(counter)
df = pd.Dataframe()
for i in range(counter):
print('\nRow #{} \n'.format(i + 1))
_xpath = xpath.format(i + 1, counter - (i + 1))
cells = driver.find_elements_by_xpath(_xpath)
if i == 0:
df = pd.DataFrame(columns=cells) # fill the dataframe with the column names
for cell in cells:
value = cell.find_element_by_xpath(".//td").text
#print(value)
if not value: # check the string is not empty
# always puting the value in the first item
df.at[i, 0] = value # put the value in the frame
df.to_csv('filename.txt') # output the dataframe to a file
Um dies zu verbessern, müssen die Elemente in einer Reihe in ein Wörterbuch eingefügt und in den Datenrahmen eingefügt werden. Aber ich schreibe das auf mein Handy, damit ich das nicht testen kann.
Mit der großen Hilfe von @Paul Brennan konnte ich den Code ändern, um die endgültige gewünschte Ausgabe zu erhalten
import pandas as pd
from selenium import webdriver
driver = webdriver.Chrome("C:/chromedriver.exe")
driver.get('file:///C:/Users/Future/Desktop/local.html')
counter = len(driver.find_elements_by_id("Section3"))
xpath = "//div[@id='Section3']/following-sibling::div[count(preceding-sibling::div[@id='Section3'])={0} and count(following-sibling::div[@id='Section3'])={1}]"
finallist = []
for i in range(counter):
#print('\nRow #{} \n'.format(i + 1))
rowlist=[]
_xpath = xpath.format(i + 1, counter - (i + 1))
cells = driver.find_elements_by_xpath(_xpath)
#if i == 0:
#df = pd.DataFrame(columns=cells) # fill the dataframe with the column names
for cell in cells:
try:
value = cell.find_element_by_xpath(".//td").text
rowlist.append(value)
except:
break
finallist.append(rowlist)
df = pd.DataFrame(finallist)
df[df.columns[[2, 0, 1, 7, 9, 8, 3, 5, 6, 4]]]
Der Code funktioniert jetzt gut, ist aber zu langsam. Gibt es eine Möglichkeit, es schneller zu machen?