Python写一个简单的爬虫

爬取的目标网站:

https://beijing.anjuke.com/sale/?pi=baidu-cpc-bj-tyong1&kwid=2341817153&utm_term=%e6%89%be%e6%88%bf&bd_vid=9128294385511928514

code

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from lxml import etree


class Main:
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        }
        self.url = "https://beijing.anjuke.com/sale/?pi=baidu-cpc-bj-tyong1&kwid=2341817153&utm_term=%e6%89%be%e6%88%bf&bd_vid=9128294385511928514"

    def lord(self):
        response = requests.get(url=self.url, headers=self.headers).text
        tree = etree.HTML(response)
        # 将页面源码数据中的房子的名称和价格进行爬取
        li_list = tree.xpath('//ul[@class="houselist-mod houselist-mod-new"]/li')
        # 将li标签表示的局部页面内容指定数据进行解析
        for li in li_list:
            title = li.xpath('./div[2]/div[1]/a/text()')[0].strip()
            describe = li.xpath('./div[2]/div[2]/span/text()')
            site = li.xpath('./div[2]/div[3]/span/text()')[0].split()[1]
            price = li.xpath('./div[3]/span[1]/strong/text()')
            print('标题:{}\n描述:{}\n地点:{}\n价格{}万\n'.format(title, describe, site, price))
            with open('date.txt','a+',encoding='utf-8') as f1:
                f1.write('标题:{}\n描述:{}\n地点:{}\n价格{}万\n\n'.format(title, describe, site, price))
                f1.close()


if __name__ == '__main__':
    obj = Main()
    obj.lord()
复制代码

输出结果

posted @   杨灏  阅读(385)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示