xpath练习

工具

pycharm

目的

'''
网站:链家网       网址:https://sh.lianjia.com/ershoufang/pudong/pg2/
用xpath做一个简单的爬虫,爬取链家网里的租房信息获取标题,位置,房屋的格局(三室一厅),关注人数,单价,总价
'''

代码

# 用xpath,需要用到lxml,利用etree.HTML,将字符串转化为Element对象,Element对象具有xpath的方法,返回结果的列表,能够接受bytes类型的数据和str类型的数据
import requests
from lxml import etree
import re
import json

house = list()
url = 'https://sh.lianjia.com/ershoufang/pudong/pg2/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'}
response = requests.get(url,headers=headers).text
# print(response)
html = etree.HTML(response)
# print(type(html))  # <class 'lxml.etree._Element'>
lis = html.xpath('//ul[@class="sellListContent"]/li')
print(lis)
for li in lis:
    item = dict()
    item['title'] = li.xpath('.//div[@class="info clear"]/div[@class="title"]/a/text()')[0].strip()
    item['position'] = li.xpath('.//div[@class="info clear"]/div[@class="flood"]//a[1]/text()')[0].strip()
    item['address'] = li.xpath('.//div[@class="info clear"]/div[@class="address"]//text()')[0].split(' | ')[0].strip()
    item['person'] = \
        re.match('\d*',li.xpath('.//div[@class="info clear"]/div[@class="followInfo"]//text()')[0].split(' / ')[0]).group()
    item['unitPrice'] = \
        li.xpath('.//div[@class="info clear"]/div[@class="priceInfo"]/div[@class="unitPrice"]//text()')[0].strip()
    item['totalPrice'] = \
        li.xpath('.//div[@class="info clear"]/div[@class="priceInfo"]'
                 '/div[@class="totalPrice totalPrice2"]/span/text()')[0].strip()
    house.append(item)

with open("house.json",'w',encoding='utf-8') as f:
    json.dump(house,f,ensure_ascii=False,indent=2)

运行结果

资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值