Python'da verileri DataFrame'e dönüştürme
@JaSON'un yardımıyla, tablodaki verileri yerel html'den almamı sağlayan bir kod ve kod selenyum kullanıyor
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)
Bu satırlar csv dosyasına aktarabileceğim geçerli bir tabloya nasıl dönüştürülebilir? İşte yerel HTML bağlantısıhttps://pastebin.com/raw/hEq8K75C
** @Paul Brennan: Sayacı düzenlemeye çalıştıktan sonra counter-1geçici olarak 18. satırın hatasını atlamak için 17 satırım var, filename.txt dosyasını aldım ve işte çıktının anlık görüntüsü
Yanıtlar
Basit bir çıktı elde etmek için kodunuzu değiştirdim. Bu, Dataframe'in vektörleştirilmiş oluşturmasını kullanmadığı için çok pitonik değildir, ancak işte nasıl çalıştığı. Önce pandaları kurun, sonra bir veri çerçevesi oluşturun (ancak sütunları henüz bilmiyoruz), ardından ilk geçişte sütunları ayarlayın (değişken sütun uzunlukları varsa bu sorunlara neden olur Sonra değerleri veri çerçevesine girin
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
Bunun nasıl daha iyi hale getirilebileceği, bir satırdaki öğeleri bir sözlüğe yerleştirmek ve bunları veri çerçevesine koymaktır. ama bunu telefonuma yazıyorum, bu yüzden test edemem.
@Paul Brennan'ın büyük yardımı ile, istenen son çıktıyı elde etmek için kodu değiştirebilirim.
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]]]
Kod şu anda iyi çalışıyor ancak çok yavaş. Daha hızlı hale getirmenin bir yolu var mı?