爬取的目标网站:

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()

输出结果

Python写一个简单的爬虫的更多相关文章

  1. 用node.js从零开始去写一个简单的爬虫

    如果你不会Python语言,正好又是一个node.js小白,看完这篇文章之后,一定会觉得受益匪浅,感受到自己又新get到了一门技能,如何用node.js从零开始去写一个简单的爬虫,十分钟时间就能搞定, ...

  2. 用Python写一个简单的Web框架

    一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...

  3. python写一个简单的CMS识别

    前言: 收集了一点cms路径,打算在写一个.之前已经写了 有需要的可以自己翻我的博客 思路: 网站添加路径判断是否为200,并且无过滤列表中的字符 代码: import requests import ...

  4. 利用python写一个简单的小爬虫 爬虫日记(1)(好好学习)

    打开py的IDLE >>>import urllib.request >>>a=urllib.request.urlopen("http://www.ba ...

  5. Python运维三十六式:用Python写一个简单的监控系统

    市面上有很多开源的监控系统:Cacti.Nagios.Zabbix.感觉都不符合我的需求,为什么不自己做一个呢? 用Python两个小时徒手撸了一个简易的监控系统,给大家分享一下,希望能对大家有所启发 ...

  6. 用python写一个简单的文件上传

    用Pycharm创建一个django项目.目录如下: <!DOCTYPE html> <html lang="en"> <head> <m ...

  7. 使用python写一个简单的C段扫

    纠结C段查询N久..刚刚拿骚棒FD去抓御剑的包,发现emmm...申请了必应的Key 然后去拿必应API查.这里疼[心]原本也想去弄的.但是人懒. 然后就没有然后了. 代码: 生成IP段的脚本图1 # ...

  8. golang写一个简单的爬虫

    package main import( "fmt" "io/ioutil" "net/http" ) func gethtml(url s ...

  9. python (1)一个简单的爬虫: python 在windows下 创建文件夹并写入文件

    1.一个简单的爬虫:爬取豆瓣的热门电影的信息 写在前面:如何创建本来存在的文件夹并写入 t_path = "d:/py/inn" #本来不存在inn,先定义路径,然后如果不存在,则 ...

随机推荐

  1. GreenPlum 提取数据表信息

    参考: https://www.alberton.info/postgresql_meta_info.html https://my.oschina.net/Kenyon/blog/226600 列信 ...

  2. Searchable 事例

    org.springframework.data.domain.Sort: org.springframework.data.domain.Pageable: //searchable框架是封装在sp ...

  3. CAS实现单点登录(SSO)经典完整教程

    转自 http://blog.csdn.net/small_love/article/details/6664831 一.简介 1.cas是有耶鲁大学研发的单点登录服务器 2.本教材所用环境 Tomc ...

  4. SciPy 介绍

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  5. HiBench成长笔记——(6) HiBench测试结果分析

    Scan Join Aggregation Scan Join Aggregation Scan Join Aggregation Scan Join Aggregation Scan Join Ag ...

  6. Python实现的远程登录windows系统功能示例

    https://www.jb51.net/article/142326.htm 重点是这几本书要好好读读!: 更多关于Python相关内容感兴趣的读者可查看本站专题:<Python进程与线程操作 ...

  7. Day4 - D - Watchcow POJ - 2230

    Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...

  8. centos7 程序快捷方式

    /usr/share/Applications 在此文件夹中,全是".desktop"的文件,利用终端打开一个看看就知道了!

  9. navicat报错2005 - Unknown MySQL server host 'localhost' (0) 原因及解决方法

    报错原因:没有连接互联网,用navicat连接本地mysql数据库,连接属性ip为localhost. 解决办法:将ip改为127.0.0.1即可.localhost是需要DNS解析后才会是127.0 ...

  10. VUE- 访问服务器端数据 axios

    VUE- 访问服务器端数据 axios 一,安装 npm install axios 二,在http.js中引入 import axios from 'axios'; 三,定义http request ...