0
点赞
收藏
分享

微信扫一扫

爬虫学习--XPath应用

一世独秀 2024-05-09 阅读 13

from lxml import etree
import requests

url = "https://xinxiang.lianjia.com/ershoufang/"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}

result = requests.get(url, headers)
html = etree.HTML(result.text)
# 标题
title = html.xpath("//div[@class='title']/a/text()")
# 位置
position = html.xpath("//div[@class='positionInfo']/a/text()")
# 地址
address = html.xpath('//div[@class="houseInfo"]/text()')
# 信息
followInfo = html.xpath('//div[@class="followInfo"]/text()')
# 价格

for i in range(len(title)):
    print("标题:", title[i], "位置:", position[2 * i] + position[2 * i + 1], "地址:", address[i], "下标:",
          followInfo[i] + "\n")


举报

相关推荐

0 条评论