среда, 5 ноября 2014 г.

Grab site by python in jquery way

A few days ago i was need parse some html and figure out how i can do this?

Well, python has many libs for this purpose: BeautifulSoup, lxml, spider.

But i've found lib that use jquery syntax for search - pyquery.
Here is official example from documentation:

#ways for parsing html
>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> import urllib
>>> d = pq("")
>>> d = pq(etree.fromstring(""))
>>> d = pq(url='http://google.com/')
>>> # d = pq(url='http://google.com/', opener=lambda url, **kw: urllib.urlopen(url).read())
>>> d = pq(filename=path_to_html_file)

>>> d("#hello")
[p#hello.hello]
>>> p = d("#hello") >>> print(p.html()) Hello world ! >>> p.html("you know Python rocks") [p#hello.hello]
>>> print(p.html()) you know Python rocks >>> print(p.text()) you know Python rocks
All needed information you can find on project page: https://pythonhosted.org/pyquery/