xpath应用(一)58同城二手房网页房屋信息爬取

Posted 离落想AC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xpath应用(一)58同城二手房网页房屋信息爬取相关的知识,希望对你有一定的参考价值。

话不多说,上代码!!!

# -*- coding: UTF-8 -*-
# @Time : 2021/5/17 10:25
# @Author : 李如旭
# @File :58tc.py
# @Software: PyCharm


import  requests
from lxml import etree
if __name__=='__main__':
    #获取数据
    headers = 
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/69.0.3947.100 Safari/537.36'
    
    url = 'https://zb.58.com/ershoufang/'
    page_text = requests.get(url=url,headers=headers).text
    #实例化对象
    tree = etree.HTML(page_text)
    #获取标签数据,标签定位
    a_list = tree.xpath('//div[@class="property"]/a')
    fp = open('./58.text','w',encoding='utf-8')
    for a in a_list:
        title = a.xpath('./div[2]/div[1]/div[1]/h3/text()')[0]
        price = a.xpath('./div[2]/div[2]/p[@class="property-price-total"]/span[1]/text()')[0]+'万'
        print(title)
        print(price)
        fp.write(title)
        fp.write(price + '\\n')

运行实现

文件查看

以上是关于xpath应用(一)58同城二手房网页房屋信息爬取的主要内容,如果未能解决你的问题,请参考以下文章

Python爬虫-利用xpath解析爬取58二手房详细信息

利用python爬取58同城简历数据

爬取58同城二手房数据存储到redis数据库和mysql数据库

xpath实战

Python高级应用程序设计任务

用Python写爬虫爬取58同城二手交易数据