แปลงข้อมูลเป็น DataFrame ใน python

Dec 08 2020

ด้วยความช่วยเหลือของ @JaSON นี่คือรหัสที่ช่วยให้ฉันได้รับข้อมูลในตารางจาก html ในเครื่องและรหัสใช้ซีลีเนียม

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)

แถวเหล่านี้จะแปลงเป็นตารางที่ถูกต้องที่ฉันสามารถส่งออกเป็นไฟล์ csv ได้อย่างไร นี่คือลิงค์ HTML ในเครื่องhttps://pastebin.com/raw/hEq8K75C

** @ Paul Brennan: หลังจากพยายามแก้ไขตัวนับให้เป็นcounter-1ฉันมี 17 แถวเพื่อข้ามข้อผิดพลาดของแถว 18 ชั่วคราวฉันได้ filename.txt และนี่คือภาพรวมของผลลัพธ์

คำตอบ

1 PaulBrennan Dec 08 2020 at 19:56

ฉันได้แก้ไขโค้ดของคุณเพื่อสร้างผลลัพธ์ง่ายๆ นี่ไม่ใช่ pythonic มากนักเนื่องจากไม่ได้ใช้การสร้าง Dataframe แบบเวกเตอร์ แต่นี่คือวิธีการทำงาน ขั้นแรกตั้งค่าแพนด้าครั้งที่สองตั้งค่าดาต้าเฟรม (แต่เรายังไม่ทราบคอลัมน์) จากนั้นตั้งค่าคอลัมน์ในพาสแรก (สิ่งนี้จะทำให้เกิดปัญหาหากมีความยาวคอลัมน์ตัวแปรจากนั้นป้อนค่าลงในดาต้าเฟรม

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

วิธีที่จะทำให้ดีขึ้นคือใส่รายการในแถวลงในพจนานุกรมแล้วใส่ลงใน datframe แต่ฉันเขียนข้อความนี้ในโทรศัพท์จึงไม่สามารถทดสอบได้

YasserKhalil Dec 11 2020 at 11:32

ด้วยความช่วยเหลือที่ดีของ @Paul Brennan ฉันสามารถแก้ไขโค้ดเพื่อให้ได้ผลลัพธ์สุดท้ายที่ต้องการ

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]]]

ตอนนี้โค้ดทำงานได้ดี แต่ช้าเกินไป มีวิธีทำให้เร็วขึ้นไหม?