爬取完会自动生成csv电子表格文件,含有房价、押付、链接等信息

环境
py2.7
pip install lxml
pip install cssselect
 
 #coding:utf-8
import csv
import urllib2
import lxml.html
import time
import sys
from lxml.cssselect import CSSSelector
import threading
reload(sys)
sys.setdefaultencoding('utf8') print "请输入要爬取得城市简称例如bj(北京):"
CITY=str(raw_input(">>>"))
def download(url, user_agent='Google', num_retries=2): headers = {'User-agent': user_agent}
request = urllib2.Request(url, headers=headers)
try:
html = urllib2.urlopen(request).read()
except urllib2.URLError as e:
html = None
if num_retries > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
return download(url, num_retries-1)
return html def get_data(url):
html_text_detail = download(url)
try:
tree = lxml.html.fromstring(html_text_detail)
house_ext = CSSSelector('div.house-pay-way > span:nth-child(3)')
house_title = CSSSelector('div.main-wrap > div.house-title > h1')
house_pay_way1 = CSSSelector('div.house-pay-way > span:nth-child(1)')
house_pay_way2 = CSSSelector('div.house-pay-way > span:nth-child(2)')
print house_title(tree)[0].text_content()
print '%s|%s' % (house_pay_way1(tree)[0].text_content(), house_pay_way2(tree)[0].text_content()) for i in range(7):
for j in range(2):
css = 'div.house-desc-item > ul.f14 > li:nth-child(%s) > span:nth-child(%s)' % (i+1, j+1)
house_info = CSSSelector(css)
data = [
('标题 : ',house_title(tree)[0].text_content(), '#',url),
('价格: ',house_pay_way1(tree)[0].text_content(), '#'),
('压付: ',house_pay_way2(tree)[0].text_content(), '#'),
('详情: ',house_info(tree)[0].text_content().replace(' ', ''), '#')]
with open('%s_houses.csv'%CITY,'ab+') as csvfile:
writer = csv.writer(csvfile,lineterminator='\n')
writer.writerows(data) except TypeError as e:
pass
except IndexError as e:
pass def get_url(html):
tree = lxml.html.fromstring(html)
sel = CSSSelector('div.mainbox > div.main > div.content > div.listBox > ul.listUl > li > div.des > h2 > a')
url_list = []
for i in sel(tree):
if i.get('href') not in url_list:
url_list.append(i.get('href'))
return url_list if __name__ == '__main__':
url_index = 'http://%s.58.com/chuzu/'%CITY
html_text_list = download(url_index)
url_list = get_url(html_text_list) for url_detail in url_list:
thr = threading.Thread(target=get_data, args=(url_detail,))
thr.start() time.sleep(0.001)

py58.py

Python 爬58同城 城市租房信息的更多相关文章

  1. 利用python爬取贝壳网租房信息

    最近准备换房子,在网站上寻找各种房源信息,看得眼花缭乱,于是想着能否将基本信息汇总起来便于查找,便用python将基本信息爬下来放到excel,这样一来就容易搜索了. 1. 利用lxml中的xpath ...

  2. Python 爬取赶集网租房信息

    代码已久,有可能需要调整 #coding:utf-8 from bs4 import BeautifulSoup #有这个bs4不用正则也可以定位要爬取的内容了 from urlparse impor ...

  3. python3.4+pyspider爬58同城(二)

    之前使用python3.4+selenium实现了爬58同城的详细信息,这次用pyspider实现,网上搜了下,目前比较流行的爬虫框架就是pyspider和scrapy,但是scrapy不支持pyth ...

  4. python爬虫项目(scrapy-redis分布式爬取房天下租房信息)

    python爬虫scrapy项目(二) 爬取目标:房天下全国租房信息网站(起始url:http://zu.fang.com/cities.aspx) 爬取内容:城市:名字:出租方式:价格:户型:面积: ...

  5. 关于python的中国历年城市天气信息爬取

    一.主题式网络爬虫设计方案(15分)1.主题式网络爬虫名称 关于python的中国城市天气网爬取 2.主题式网络爬虫爬取的内容与数据特征分析 爬取中国天气网各个城市每年各个月份的天气数据, 包括最高城 ...

  6. Python爬取房天下二手房信息

    一.相关知识 BeautifulSoup4使用 python将信息写入csv import csv with open("11.csv","w") as csv ...

  7. python爬取北京政府信件信息01

    python爬取,找到目标地址,开始研究网页代码格式,于是就开始根据之前学的知识进行爬取,出师不利啊,一开始爬取就出现了个问题,这是之前是没有遇到过的,明明地址没问题,就是显示网页不存在,于是就在百度 ...

  8. 用Python爬取智联招聘信息做职业规划

    上学期在实验室发表时写了一个爬取智联招牌信息的爬虫. 操作流程大致分为:信息爬取——数据结构化——存入数据库——所需技能等分词统计——数据可视化 1.数据爬取 job = "通信工程师&qu ...

  9. python爬取 “得到” App 电子书信息

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 静觅 崔庆才 PS:如有需要Python学习资料的小伙伴可以加点击下 ...

随机推荐

  1. ECharts访问后台,JSON格式返回数据实例

    完成图 一.页面代码 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...

  2. top 常用

    top -c 查看进程 同时 shift +m 内存倒序

  3. python基础提高演练(名片管理系统的开发)

    综合应用 —— 名片管理系统 目标 综合应用已经学习过的知识点: 变量 流程控制 函数 模块 开发 名片管理系统 系统需求 1. 程序启动,显示名片管理系统欢迎界面,并显示功能菜单 ********* ...

  4. 2017年3月30日15:00:19 fq以后的以后 动态代理

    代理与继承,组合不同的是,继承是继承父类特性,组合是拼装组合类的特性,代理是使用代理类的指定方法并可以做自定义. 静态类是应用单个类,当代理的类数量较多时可用动态代理,动态代理在概念上很好理解 htt ...

  5. VS2013/VS2015/VS2017通过oschina托管代码

    1.到http://git.oschina.net注册账号,创建项目,这一步不做详细描述,创建后效果如图 2.打开VS创建项目,为了测试,创建一个空白解决方案 3.把解决方案添加到源代码管理(这一步仅 ...

  6. Linux下不停止服务,清空nohup.out文件

    转载:http://www.sucheasy.com/OracleFusionMiddleware/640.html 1.nohup.out的由来及作用 用途:LINUX命令用法,不挂断地运行命令. ...

  7. centos7 安装php7

    方法一 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh https:/ ...

  8. vue-router(配置子路由--单页面多路由区域操作)

    1.配置子路由: import Post from "@components/Post" export default new Router({ routers:[ { path: ...

  9. Ubuntu下math库函数编译时未定义问题的解决

    自己在Ubuntu下练习C程序时,用到了库函数math.h,虽然在源程序中已添加头文件“math.h”,但仍提示所用函数未定义,原本以为是程序出错了,找了好久,这是怎么回事呢? 后来上网查了下,发现是 ...

  10. MYSQL之IFNULL

    IFNULL是MYSQL中的一个判断字段值是否为null的函数,如果为null,则此查询可以变更为指定的值: 如 IFNULL(a,0)  如果a为null,则查询出0. 如果A表中没有数据 sele ...