python-parsel
Parsel是壹個使用XPath和CSS選擇器(可選地與正則表達式結合)從HTML和XML提取數據的庫
壹、安裝
官網:https://pypi.org/project/parsel/
pip安裝:pip install parsel 默認安裝的是最新版
pip install parsel=1.6.0 目前官方最新版本
PyCharm:File =》Setting =》Project:sintemple =》 Project:Interpreter =》點擊右上角的加號(或者按快捷鍵Alt+Insert)=》在輸入框中輸入parsel,會出現壹個只有parsel的壹列,點擊選擇它 =》Install Package 等待安裝完成就可以了(註:其中Specify version選中可以在下拉框中選擇版本)
————————————————
二、Selector
Selector(text=None, type=None, namespaces=None, root=None,base_url=None, _expr=None)
創建解析HTML或XML文本的對象
參數:
text 在python2中是壹個Unicode對象,在python3中是壹個str對象
type 定義Selector類型,可以是"html",“xml"或者是None(默認),如果為None則默認選擇為"html”
base_url allows setting a URL for the document. This is needed when looking up external entities with relative paths(允許為文檔設置URL。在使用相對路徑查找外部實體時,這是必需的)
Selector的對象方法
①. Selector.attrib()
返回基礎元素的屬性字典
②. Selector.css(query)
css選擇器
③. Selector.get()
序列化並以單個unicode字符串返回匹配的節點
④. Selector.getall()
序列化並以第1個元素的unicode字符串列表返回匹配的節點
⑤. Selector.re(self, regex, replace_entities=True)
正則選擇器
⑥. Selector.re_first(self, regex, default=None, replace_entities=True)
If the list is empty or the regex doesn’t match anything, return the default value (None if the argument is not provided)如果列表為空或正則表達式不匹配任何東西,返回默認值(如果沒有提供參數,則返回’None’ )
⑦. Selector.remove()
Remove matched nodes from the parent for each element in this list.從父節點中刪除列表中每個元素的匹配節點。
⑧. Selector.xpath(self, query, namespaces=None, **kwargs)
xpath選擇器
SelectorList的對象方法
SelectorList類是內置list類的壹個子類,它提供了壹些額外的方法。
①. attrib 返回第壹個元素的屬性字典。如果列表為空,則返回空dict
②. css(query) .css()對該列表中的每個元素調用方法,然後將其結果展平為另壹個SelectorList。query 與 Selector.css()
③. extract() 調用.get()此列表中每個元素的方法,並將其結果展平,以unicode字符串列表形式返回。
④. extract_first(default=None) 返回.get()此列表中第壹個元素的結果。如果列表為空,則返回默認值。
⑤. get(default=None) 返回.get()此列表中第壹個元素的結果。如果列表為空,則返回默認值。
⑥. getall() 調用.get()此列表中每個元素的方法,並將其結果展平,以unicode字符串列表形式返回。
⑦. re(regex, replace_entities=True) 調用.re()此列表中每個元素的方法,並將其結果展平,以unicode字符串列表形式返回。默認情況下,字符實體引用由其對應的字符替換(&和和除外<。以傳遞replace_entities,False關閉這些替換。
⑧. re_first(regex, default=None, replace_entities=True) 調用.re()此列表中第壹個元素的方法,並以Unicode字符串返回結果。如果列表為空或正則表達式不匹配任何內容,則返回默認值(None如果未提供參數)。默認情況下,字符實體引用由其對應的字符替換(&和和除外<。以傳遞replace_entities,False關閉這些替換。
⑨. remove() 從父級中刪除此列表中每個元素的匹配節點。
⑩. xpath(xpath, namespaces=None, **kwargs) .xpath()對該列表中的每個元素調用方法,然後將其結果展平為另壹個SelectorList。query 與 Selector.xpath()namespaces是用於將其他前綴添加到已註冊的前綴的可選映射(字典)。與相對,這些前綴不會保存以備將來使用。
舉例說明:
html代碼
————————————————
三、csstranslator
TranslatorMixin
This mixin adds support to CSS pseudo elements via dynamic dispatch.Currently supported pseudo-elements are ::text and ::attr(ATTR_NAME).
①. xpath_attr_functional_pseudo_element(xpath, function)
Support selecting attribute values using ::attr() pseudo-element
②. xpath_element(selector)
③. xpath_pseudo_element(xpath, pseudo_element)
Dispatch method that transforms XPath to support pseudo-element
④. xpath_text_simple_pseudo_element(xpath)
Support selecting text nodes using ::text pseudo-element
XPathExpr(path=’’, element=’*’, condition=’’, star_prefix=False)
GenericTranslator
HTMLTranslator(xhtml=False)
四、utils
extract_regex(regex, text, replace_entities=True)
Extract a list of unicode strings from the given text/encoding using the following policies: * if the regex contains a named group called “extract” that will be returned * if the regex contains multiple numbered groups, all those will be returned (flattened) * if the regex doesn’t contain any group the entire regex matching is returned
flatten(sequence) → list
Returns a single, flat list which contains all elements retrieved from the sequence and all recursively contained sub-sequences (iterables). Examples: >>> [1, 2, [3,4], (5,6)] [1, 2, [3, 4], (5, 6)] >>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, (8,9,10)]) [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10] >>> flatten([“foo”, “bar”]) [‘foo’, ‘bar’] >>> flatten([“foo”, [“baz”, 42], “bar”]) [‘foo’, ‘baz’, 42, ‘bar’]
iflatten(sequence) → Iterator
Similar to .flatten(), but returns iterator instead
shorten(text, width, suffix=’…’)
Truncate the given text to fit in the given width.
————————————————
原文鏈接:網頁鏈接