用xpath写一个python爬取二手房网页数据
时间: 2023-12-15 19:05:09 浏览: 93
以下是一个简单的Python程序,使用XPath从二手房网站抓取数据:
```python
import requests
from lxml import etree
# 网站地址
url = "http://www.example.com/ershoufang"
# 发送请求
response = requests.get(url)
# 解析HTML
html = etree.HTML(response.text)
# 得到所有房子信息的列表
house_list = html.xpath('//div[@class="house-info"]')
# 遍历列表,提取房子信息
for house in house_list:
# 房子标题
title = house.xpath('.//a[@class="house-title"]/text()')[0]
# 房子总价
total_price = house.xpath('.//div[@class="total-price"]/text()')[0]
# 房子单价
unit_price = house.xpath('.//div[@class="unit-price"]/text()')[0]
# 房子面积
area = house.xpath('.//div[@class="area"]/text()')[0]
# 输出房子信息
print(title, total_price, unit_price, area)
```
以上代码仅供参考,具体情况需要根据实际网站进行调整。
阅读全文