\'\'\'
/从根标签开始
//从当前标签  后续节点含有即可选出
*通配符,选择所有
//div/book[1]/  选择div下第一个book标签的 元素
//div/book/ [@lang=\'zh\'] 选择 属性含有lang且内容是zh的 元素
//div/book/  //book/  //  具有相同的结果,因为使用相对路径最终都指向 
//book/ /@* 将 所有属性值选择出来
//book/ /text() 将 的内容选择出来,使用内置text()函数
//a[@href=\" 1.html\" and @id=\"places_neighbours_row\"]
//a[@href=\" 1.html\" or @id=\"places_neighbours_row\"]
//div/book[last()]/ /text() 将最后一个book元素选出
//div/book[price > 39]/  将book子标签price数值大于39的选择出来
//li[starts-with(@class,\'item\')] 将class属性前缀是item的li选择出来
// [contains(@lang,\'eng\')] 将 属性lang含有eng关键字的标签选出来
\'\'\'
import l .html
import requests

网页内容:
\'<tr id=\"places_population__row\"><td class=\"w2p_fl\"><label class=\"readonly\" for=\"places_population\" id=\"places_population__label\">Population: </label></td><td class=\"w2p_fw\">84,000</td><td class=\"w2p_fc\"></td></tr>\'

result = requests.get(\'http://example.webscraping.com/places/default/view/Andorra-6\')
html = l .html.fromstring(result.content.decode(\'utf-8\'))
html_data = html.xpath(\'//tr[@id=\"places_population__row\"]/td[@class=\"w2p_fw\"]\')
print(html_data)
for i in html_data:
    print(i.text)
收藏 打印