Python Api¶
This is the technical documentation of the python bindins libphantompy‘s API.
Context & Config¶
Context
class represents a context singleton
pointer that contains an instance of a QT application, an interface for some
WebKit engine configuration options and some actions (e.g. clear cache memory).
-
class
phantompy.context.
Config
¶ WebKit engine configuration interface.
This class should onlu be accessed throught
Context
instance and can not be instanciated as is.This config interface exposes these properties:
- ‘load_images’ (bool)
- ‘javascript’ (bool)
- ‘dns_prefetching’ (bool)
- ‘plugins’ (bool)
- ‘private_browsing’ (bool)
- ‘offline_storage_db’ (bool)
- ‘offline_storage_quota’ (int)
- ‘frame_flattening’ (bool)
- ‘local_storage’ (boo)
And some additional methods:
-
set_max_pages_in_cache
(num)¶ Set webkit page number to maintain in cache.
-
set_object_cache_capacity
(min_dead_capacity, max_dead, total_capacity)¶ Set webkit object cache capacity.
-
class
phantompy.context.
Context
¶ Clear all cookies.
-
clear_memory_caches
()¶ Clear all memory used by webkit engine.
Get all available cookies.
-
process_events
(timeout=200)¶ Method like a time.sleep but while waiths a timeout process qt events.
Generic method for set cookie to the cookiejar instance of WebKit Engine.
Parameters: - name (str) – cookie name.
- value (str) – cookie value.
- domain (str) – cookie domain.
- path (str) – cookie path (default ‘/’)
- path – cookie expires date (this must be datetime, date, timedelta or str type.
Return type: None
Set a list of cookies.
-
set_headers
(headers)¶ Set a list of headers.
-
phantompy.context.
context
()¶ Get or create instance of context (singleton).
-
phantompy.context.
destroy_context
()¶ Destroy context singleton instance.
Web Element¶
Live DOM manipulation and transversing api.
-
class
phantompy.webelements.
WebElement
(el_ptr, frame)¶ Class that represents a live dom element on webkit engine.
-
append
(element)¶ Append element or raw html to the current dom element.
Parameters: element – Unicode string with html or WebElement instance. Return type: None Example:
>>> element = p.cssselect("body > section")[0] >>> element.append("<span>{0}</span>".format("FOO"))
-
cssselect
(*args, **kwargs)¶ Find all descendent elements by css selector like jQuery.
Parameters: selector (str) – jQuery like selector Return type: list
-
cssselect_first
(selector)¶
-
frame
¶ Returns a frame instance of this element.
-
get_attr
(name, **kwargs)¶
-
get_attrs
(*args, **kwargs)¶ Get all attributes as python dict. :rtype: dict
-
get_classes
()¶ Returs a list of classes that hace current dom element.
Example:
>>> element = p.cssselect("section")[0] >>> element.get_classes() ["main", "main-section"]
-
has_attr
(attrname)¶ Method that checks the existence of one concrete attribute by name.
Parameters: attribute (str) – attribute name Return type: bool
-
has_attrs
()¶ Method that checkos of existence of any attrs. Returns a True value if a current dom element has any attribute.
Return type: bool
-
has_class
(classname)¶ Method that checks the existense of some class in a current dom element.
Parameters: classname (str) – class name Return type: bool Example:
>>> element = p.cssselect("section")[0].has_class("foo") False
-
inner_html
()¶ Get inner dom structure as html.
Return type: str
-
inner_text
()¶ Get inner dom structure as text, stripping all html tags.
Return type: str
-
is_none
()¶ Checks if a current dom element is empty or not.
Return type: bool
-
name
¶ Returns a tagname.
-
next
()¶ Get a next element in the same level of dom.
Return type: WebElement
-
prev
()¶ Get a previous element in the same level of dom.
Return type: WebElement
-
ptr
¶ Returns a pointer to internal C++ instance object.
-
remove
()¶ Remove the current element from the living dom and make this element as empty element.
-
remove_attr
(attrname)¶ Remove attribute by name.
Parameters: attrname (str) – attribute name. Return type: None
-
remove_childs
()¶ Remove all childs of the current dom.
-
remove_class
(classname)¶ Method that removes a class from a current dom node. If a class does not exists, this method does nothink.
Parameters: classname (str) – class name Return type: None
-
replace
(element)¶ Replace the current element with other.
Parameters: element – Unicode string with html or WebElement
instance.Return type: None
-
set_attr
(name, value)¶
-
set_attrs
(attrs)¶
-
wrap
(element)¶ Wraps the current element with other element.
Parameters: element – Unicode string with html or WebElement
instance.Return type: None Example:
>>> element = p.cssselect("a")[0] >>> element.wrap("<div/>")
-