我想使用xml解析一个网页。已经到了这一步:
tree=etree.HTML(text)
ob=tree.xpath('//*[@id="comic"]')[0]
ob是这样的:
<div>
<src="url".........>
<src="url".........>
......
<div>
然后我想要在ob里面提取src后面的url
我试过用ob.xpath('//@src'),但得到的不是ob里面的src,而是整个页面的所有src
请问应该怎么做?
from lxml import etree
html = """<div>
<img src="https://www.tianmao.com">
</div>
<div class="button">
<img src="https://www.baidu.com">
<img src="https://www.jd.com">
<img src="https://www.taobao.com">
<img src="https://www.aiqiyi.com">
</div>"""
the_html = etree.HTML(html)
res = the_html.xpath(r'.//div[@class="button"]//@src')
print(res)
我们可以更具父标签的一些属性来确定位置,我的例子里面就是class