כלים ללכידה ולהמרה של האינטרנט

ללכוד טבלאות HTML מאתרי אינטרנט עם פייתון

ממשק API של פייתון

ישנן מספר דרכים להמיר טבלאות HTML into גיליונות אלקטרוניים של CSV ו- Excel באמצעות ממשק ה- API של GrabzIt Pythonלהלן כמה מהטכניקות השימושיות ביותר. עם זאת לפני שתתחיל לזכור את זה לאחר התקשר אל URLToTable, HTMLToTable or FileToTable שיטות Save or SaveTo יש לקרוא לשיטה ללכידת הטבלה. אם אתה רוצה לראות במהירות אם שירות זה מתאים לך, אתה יכול לנסות הדגמה חיה של לכידת טבלאות HTML מכתובת אתר.

אפשרויות בסיסיות

קטע הקוד שלהלן ממיר אוטומטית את טבלת ה- HTML הראשונה בדף אינטרנט שצוין intoa מסמך CSV שאפשר להוריד או לנתח.

grabzIt.URLToTable("https://www.tesla.com")
# Then call the Save or SaveTo method
grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>")
# Then call the Save or SaveTo method
grabzIt.FileToTable("tables.html")
# Then call the Save or SaveTo method

כברירת מחדל זה ימיר את הטבלה הראשונה שהיא מזהה intoa שולחן. עם זאת ניתן להמיר את הטבלה השנייה בדף אינטרנט על ידי העברת 2 לטבלה tableNumberToInclude תכונה.

from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.tableNumberToInclude = 2

grabzIt.URLToTable("https://www.tesla.com", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.tableNumberToInclude = 2

grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.tableNumberToInclude = 2

grabzIt.FileToTable("tables.html", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv")

אתה יכול גם לציין את targetElement תכונה שתבטיח רק טבלאות בתוך מזהה האלמנט שצוין יומרו.

from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.targetElement = "stocks_table"

grabzIt.URLToTable("https://www.tesla.com", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.targetElement = "stocks_table"

grabzIt.HTMLToTable("<html><body><table id='stocks_table'><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.targetElement = "stocks_table"

grabzIt.FileToTable("tables.html", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv")

לחלופין אתה יכול ללכוד את כל הטבלאות בדף אינטרנט על ידי העברת נאמן ל- includeAllTables עם זאת, זה יעבוד רק בפורמטים XLSX ו- JSON. אפשרות זו תכניס כל טבלה לגיליון חדש בתוך חוברת העבודה של הגיליון האלקטרוני שנוצר.

from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.format = 'xlsx'
options.includeAllTables = True

grabzIt.URLToTable("https://www.tesla.com", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.xlsx")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.format = 'xlsx'
options.includeAllTables = True

grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.xlsx")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.format = 'xlsx'
options.includeAllTables = True

grabzIt.FileToTable("tables.html", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.xlsx")

המרת טבלאות HTML ל- JSON

השימוש בשירות המרות הטבלאות של Python ו- GrabzIt מאפשר להמיר טבלאות HTML into JSON. השלב הראשון כמוצג להלן הוא לפרט json בפרמטר הפורמט. לאחר מכן אנו מקבלים את JSON string באופן סינכרוני עם SaveTo שיטה, לאחר מכן תוכל להשתמש בנתח JSON המועדף עליך עבור Python כדי להמיר את JSON string intאובייקט.

from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.format = "json"
options.tableNumberToInclude = 1

grabzIt.URLToTable("https://www.tesla.com", options)

json = grabzIt.SaveTo()
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.format = "json"
options.tableNumberToInclude = 1

grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options)

json = grabzIt.SaveTo()
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.format = "json"
options.tableNumberToInclude = 1

grabzIt.FileToTable("tables.html", options)

json = grabzIt.SaveTo()

מזהה מותאם אישית

אתה יכול להעביר מזהה מותאם אישית אל ה- שולחן שיטות כמוצג להלן, ערך זה מוחזר לאחר מכן למטפל GrabzIt Python שלך. לדוגמה, מזהה מותאם אישית זה יכול להיות מזהה בסיס נתונים, המאפשר לשייך צילום מסך לרשומת מסד נתונים מסוימת.

from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.customId = "123456"

grabzIt.URLToTable("https://www.tesla.com", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.customId = "123456"

grabzIt.HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItTableOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItTableOptions.GrabzItTableOptions()
options.customId = "123456"

grabzIt.FileToTable("example.html", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")