Python 爬58同城 城市租房信息
爬取完会自动生成csv电子表格文件,含有房价、押付、链接等信息
#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同城 城市租房信息的更多相关文章
- 利用python爬取贝壳网租房信息
最近准备换房子,在网站上寻找各种房源信息,看得眼花缭乱,于是想着能否将基本信息汇总起来便于查找,便用python将基本信息爬下来放到excel,这样一来就容易搜索了. 1. 利用lxml中的xpath ...
- Python 爬取赶集网租房信息
代码已久,有可能需要调整 #coding:utf-8 from bs4 import BeautifulSoup #有这个bs4不用正则也可以定位要爬取的内容了 from urlparse impor ...
- python3.4+pyspider爬58同城(二)
之前使用python3.4+selenium实现了爬58同城的详细信息,这次用pyspider实现,网上搜了下,目前比较流行的爬虫框架就是pyspider和scrapy,但是scrapy不支持pyth ...
- python爬虫项目(scrapy-redis分布式爬取房天下租房信息)
python爬虫scrapy项目(二) 爬取目标:房天下全国租房信息网站(起始url:http://zu.fang.com/cities.aspx) 爬取内容:城市:名字:出租方式:价格:户型:面积: ...
- 关于python的中国历年城市天气信息爬取
一.主题式网络爬虫设计方案(15分)1.主题式网络爬虫名称 关于python的中国城市天气网爬取 2.主题式网络爬虫爬取的内容与数据特征分析 爬取中国天气网各个城市每年各个月份的天气数据, 包括最高城 ...
- Python爬取房天下二手房信息
一.相关知识 BeautifulSoup4使用 python将信息写入csv import csv with open("11.csv","w") as csv ...
- python爬取北京政府信件信息01
python爬取,找到目标地址,开始研究网页代码格式,于是就开始根据之前学的知识进行爬取,出师不利啊,一开始爬取就出现了个问题,这是之前是没有遇到过的,明明地址没问题,就是显示网页不存在,于是就在百度 ...
- 用Python爬取智联招聘信息做职业规划
上学期在实验室发表时写了一个爬取智联招牌信息的爬虫. 操作流程大致分为:信息爬取——数据结构化——存入数据库——所需技能等分词统计——数据可视化 1.数据爬取 job = "通信工程师&qu ...
- python爬取 “得到” App 电子书信息
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 静觅 崔庆才 PS:如有需要Python学习资料的小伙伴可以加点击下 ...
随机推荐
- 谈谈我对Linux系统学习的历程回顾
众所周知,Windows 和Linux 是目前最流行的2个操作系统.Windows系统适合普通用户,它的优势是图形化界面,简单易用,使用起来门槛很低,很容易上手,所以,windows占有了大多数普 ...
- vue全家桶(vue-cli,vue-router,vue-resource,vuex)-1
vue-cli # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue init webpack my- ...
- centos7源码安装Python3的前提条件
centos7源码安装Python3的前提条件: # yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline- ...
- FS:[0] 链条
0x01 用户态 在x86系统中,当线程在用户态执行时,段寄存器fs总是指向当前线程的TEB. 在Ntdll中有一个未公开的函数NtCurrentTeb() ,用来取得当前线程的TEB地址.FS:[ ...
- C语言结构体指针初始化(转)
reference: https://www.cnblogs.com/losesea/archive/2012/11/15/2772526.html 今天来讨论一下C中的内存管理. 记得上周在饭桌上和 ...
- Effective java第17条:要么为继承而设计,并提供文档说明,要么就禁止继承
不要过度设计. 面向对象编程,从一开始被洗脑难免在上手写代码时都会首先思考有没有公共方法啊,能不能把两个类抽象成一个父类再继承啊等,慎重使用继承,当要使用继承时一定要在文档注释中写明重写这个方法会给其 ...
- 2019-04-17-day034-线程与数据共享
内容回顾 锁 互斥锁 能够保护数据的安全性 保证对于数据的修改操作同一时刻多个进程只有一个进程执行 进程数据不安全 : 同时修改文件/数据库/其他共享资源的数据 ###队列 -- 实现了进程之间的通信 ...
- python day19--面向对象,接口,封装
#1.接口类,对象类.规范类中方法的统一. # 第一版:三个类,每个类中有相同的方法 # class Alipay: # def __init__(self,money): # self.money= ...
- linux do{} while(0)
do{}while(0) 在linux中,经常会看到do{}while(0)这样的语句,许多人开始都会疑惑,认为do{}while(0)毫无意义,因为它只会执行一次,加不加do{}while(0)效果 ...
- ggplot2 multiply graphs on one figure
library(ggplot2)library(grid)grid.newpage()pushViewport(viewport(layout = grid.layout(1,2)))vplayout ...