\'\'\'
/从根标签开始
//从当前标签 后续节点含有即可选出
*通配符,选择所有
//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)
继续阅读与本文标签相同的文章
上一篇 :
云栖大会惊现体感互娱 视频云能力全面释放
下一篇 :
当梵高遇到上专有云-神经网络作画
-
Docker 用ansible给主机安装docker
2026-05-18栏目: 教程
-
Docker centos7 with sshd (sshd centos7 常用镜像)
2026-05-18栏目: 教程
-
Docker 做资源限制
2026-05-18栏目: 教程
-
JavaScript 基础类型,数据类型
2026-05-18栏目: 教程
-
[MySQL] docker下安装使用mysql配置主从复制
2026-05-18栏目: 教程
