xpath提取二手房
#利用xpath提取二手房
import requests
from lxml import etree
url='https://bj.58.com/ershoufang/'
head={
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.40'
}
htmltext=requests.get(url=url,headers=head).text
parse=etree.HTMLParser(encoding='utf-8')
tree=etree.HTML(htmltext,parser=parse)
l=tree.xpath('/body/div/@id="__nuxt"/div/@id="__layout"/div/@class="list"/section/@class="list-body"/section/@class="list-main"/section/@class="list-left"/section/@class="list"/a/@class="property-ex"/div/@class="property-content"/div/@class="property-content-detail"/div/@class="property-content-title"/h3/text()')
print(l)