匹配规则:
表达式 | 描述 |
nodename | 选取此节点的所有子节点 |
/ | 从当前节点选取直接子节点 |
// | 从当前节点选取子孙节点 |
. | 选取当前节点 |
.. | 选取当前节点的父节点 |
@ | 选取属性 |
import requests
from lxml import etree
if __name__=='__main__':
url="https://xy.58.com/chuzu/0/?PGTID=0d200001-021f-6c94-ad3a-7f104ec104d3&ClickID=1"
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
"cookie": "f=n; 请登录后抓取xxzl_smartid=59952ba7da958c3601dd4c0997c63976"
}
#获取58个人房源信息
page_text=requests.get(url=url,headers=headers).text
#使用etree 载入html
tree=etree.HTML(page_text)
lst=tree.xpath('//li[@class="house-cell "]')
for li in lst:
title=li.xpath('./div[@class="des"]/h2/a/text()')[0]
price=li.xpath('./div[@class="list-li-right"]/div[@class="money"]/b/text()')[0]
hostType=li.xpath('./div[@class="des"]/p[1]/text()')[0].strip()
xiaoqu = li.xpath('./div[@class="des"]/p[2]//text()')[1].strip()
print(title,price,hostType,xiaoqu)