Example of using the Xpath expression in urllib, urllibxpath
Example of using Xpath expressions in urllib
To use an xpath expression, you must first convert the data to be matched to the tree format. Therefore, you must first install the lxml module. You can use pip to install the SDK.
Sample Code:
Import urllib. requestfrom lxml import etree date = urllib. request. urlopen ("http://www.sohu.com /"). read (). decode ("UTF-8", "ignore") treedate = etree. HTML (date) # convert the data obtained by urllib to the etree structure title = treedate. xpath ("// title/text ()") # use an xpath expression to obtain the expected data if (str (type (title ))) = "<class 'LIST'>": # This avoids the format of some data, such as the iterator. Therefore, you can determine the acquired data type and convert it to the list type passelse: title = [I for I in title] # convert data to list type through Traversal
Additional knowledge points:
>>> [I for I in range (1, 10)] [1, 2, 3, 4, 5, 6, 7, 8, 9]