PyQuery is a jQuery-like Python library that provides a powerful tool for parsing and manipulating HTML documents. How to use the PyQuery library, the following is a detailed description of using the PyQuery library:
Install PyQuery
The PyQuery library can be installed via pip. Run the following command in terminal to install:
pip install pyquery
Import PyQuery
To use PyQuery, this library needs to be imported. PyQuery can be imported using the following code:
from pyquery import PyQuery as pq
get HTML document
Use pq()
the function to initialize the HTML document, which can be obtained from several different sources:
- URL
- document
- string
Here is an example:
Get HTML document from URL
doc = pq(url='http://www.baidu.com')
print(doc('title'))
Get HTML document from file
doc = pq(filename='example.html')
print(doc('title'))
Get HTML document from string
doc = pq('<html><head><