爬取的网站:http://jbk.39.net/chancegz/
这里只针对个别属性值:
#例如:'别名'下的span标签文本,‘发病部位’下的span标签文本以及‘挂号科室‘下的span标签文本
#
def disease(url):
text = get_html(url)
tree = etree.HTML(text)
bm = tree.xpath('//ul[@class="information_ul"]/li/i[text()="别名:"]/following-sibling::span/text()')
bw = tree.xpath('//ul[@class="information_ul"]/li/i[text()="发病部位:"]/following-sibling::span/a/text()')
ks = tree.xpath('//ul[@class="information_ul"]/li/i[text()="挂号科室:"]/following-sibling::span/a/text()')
return bm, bw, ks
补充其他:
# /从根元素开始,相当于绝对路径
print(tree.xpath('/html/body/ul'))
# //全局搜索,找到所有
print(tree.xpath('//li'))
ul = tree.xpath('//ul')
# . 当前
# 返回的都是列表,查找到所有
li = ul[0].xpath('./li')
print(li)
for l in li:
# 获取属性id的值 @id
print(l.xpath('./@id'))
# 定位 /标签[@属性='值']
liClass = tree.xpath("//li[@class='liClass']")
print(liClass)
#判断,@属性='值' --->返回True或False
print(tree.xpath("//li/@id='12'