- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python爬虫租房信息在地图上显示的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本人初学python是菜鸟级,写的不好勿喷.
python爬虫用了比较简单的urllib.parse和requests,把爬来的数据显示在地图上。接下里我们话不多说直接上代码
1.安装python环境和编辑器(自行度娘) 。
2.本人以58品牌公寓为例,爬取在杭州地区价格在2000-4000的公寓.
1
2
3
4
5
6
|
#-*- coding:utf-8 -*-
from
bs4
import
beautifulsoup
from
urllib.parse
import
urljoin
import
requests
import
csv
import
time
|
以上是需要引入的模块 。
1
2
3
|
url
=
"http://hz.58.com/pinpaigongyu/pn/{page}/?minprice=2000_4000"
#已完成的页数序号,初时为0
page
=
0
|
以上的全局变量 。
1
2
|
csv_file
=
open
(r
"c:\users\****\desktop\houosenew.csv"
,
"a+"
,newline
=
'')
csv_writer
=
csv.writer(csv_file, delimiter
=
','
)
|
自定义某个位置来保存爬取得数据,本人把爬取得数据保存为csv格式便于编辑(其中”a+”表示可以多次累加编辑在后面插入数据,建议不要使用“wb”哦!newline=”表示没有隔行) 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
while
true:
#为了防止网站屏蔽ip,设置了时间定时器每隔5秒爬一下。打完一局农药差不多都爬取过来了。
time.sleep(
5
)
page
+
=
1
#替换url中page变量
print
(url.
format
(page
=
page)
+
"ok"
)
response
=
requests.get(url.
format
(page
=
page))
html
=
beautifulsoup(response.text)
#寻找html中dom节点li
house_list
=
html.select(
".list > li"
)
# 循环在读不到新的房源时结束
if
not
house_list:
break
for
house
in
house_list:
#根据hml的dom节点获取自己需要的数据
house_title
=
house.select(
"h2"
)[
0
].string
house_url
=
urljoin(url, house.select(
"a"
)[
0
][
"href"
])
house_pic
=
urljoin(url, house.select(
"img"
)[
0
][
"lazy_src"
])
house_info_list
=
house_title.split()
# 如果第一列是公寓名 则取第二列作为地址
if
"公寓"
in
house_info_list[
0
]
or
"青年社区"
in
house_info_list[
0
]:
house_location
=
house_info_list[
0
]
else
:
house_location
=
house_info_list[
1
]
house_money
=
house.select(
".money"
)[
0
].select(
"b"
)[
0
].string
csv_writer.writerow([house_title, house_location, house_money,house_pic ,house_url])
#最后不要忘记关闭节流
csv_file.close()
|
如果网站屏蔽了你的ip,你可以做一个ip地址数组放在http的头部具体度娘一下吧.
接下来我们写html 。
只是简单的写了一下写的不好见谅。用的是高德地图,具体的js api可以到高德开发者上去看.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
<body>
<div
id
=
"container"
><
/
div>
<div
class
=
"control-panel"
>
<div
class
=
"control-entry"
>
<label>选择工作地点:<
/
label>
<div
class
=
"control-input"
>
<
input
id
=
"work-location"
type
=
"text"
>
<
/
div>
<
/
div>
<div
class
=
"control-entry"
>
<label>选择通勤方式:<
/
label>
<div
class
=
"control-input"
>
<
input
type
=
"radio"
name
=
"vehicle"
value
=
"subway,bus"
onclick
=
"takebus(this)"
checked
/
> 公交
+
地铁
<
input
type
=
"radio"
name
=
"vehicle"
value
=
"subway"
onclick
=
"takesubway(this)"
/
> 地铁
<
input
type
=
"radio"
name
=
"vehicle"
value
=
"walk"
onclick
=
"takewalk(this)"
/
> 走路
<
input
type
=
"radio"
name
=
"vehicle"
value
=
"bike"
onclick
=
"takebike(this)"
/
> 骑车
<
/
div>
<
/
div>
<div
class
=
"control-entry"
>
<label>导入房源文件:<
/
label>
<div
class
=
"control-input"
>
<
input
type
=
"file"
name
=
"file"
id
=
"filecsv"
/
>
<button style
=
"margin-top: 10px;width: 50%;"
onclick
=
"changecsv()"
>开始<
/
button>
<
/
div>
<
/
div>
<
/
div>
<div
id
=
"transfer-panel"
><
/
div>
<script>
var
map
=
new amap.
map
(
"container"
, {
resizeenable: true,
zoomenable: true,
center: [
120.1256856402492
,
30.27289264553506
],
zoom:
12
});
/
/
添加标尺
var scale
=
new amap.scale();
map
.addcontrol(scale);
/
/
公交到达圈对象
var arrivalrange
=
new amap.arrivalrange();
/
/
经度,纬度,时间(用不到),通勤方式(默认是地铁+公交
+
走路
+
骑车)
var x, y, t, vehicle
=
"subway,bus"
;
/
/
工作地点,工作标记
var workaddress, workmarker;
/
/
房源标记队列
var rentmarkerarray
=
[];
/
/
多边形队列,存储公交到达的计算结果
var polygonarray
=
[];
/
/
路径规划
var amaptransfer;
/
/
信息窗体对象
var infowindow
=
new amap.infowindow({
offset: new amap.pixel(
0
,
-
30
)
});
/
/
地址补完的使用
var auto
=
new amap.autocomplete({
/
/
通过
id
指定输入元素
input
:
"work-location"
});
/
/
添加事件监听,在选择补完的地址后调用worklocationselected
amap.event.addlistener(auto,
"select"
, worklocationselected);
function takebus(radio) {
vehicle
=
radio.value;
loadworklocation()
}
function takesubway(radio) {
vehicle
=
radio.value;
loadworklocation()
}
function takewalk(radio){
vehicle
=
radio.value;
loadworklocation()
}
function takebike(radio) {
vehicle
=
radio.value;
loadworklocation()
}
/
/
获取加载的文件
function changecsv() {
$(
"#filecsv"
).csv2arr(function (res) {
$.each(res, function (k, p) {
if
(res[k][
1
]) {
/
/
addmarkerbyaddress(地址,价格,展示的图片)
addmarkerbyaddress(res[k][
1
], res[k][
2
],res[k][
3
])
}
})
});
}
function worklocationselected(e) {
workaddress
=
e.poi.name;
loadworklocation();
}
function loadworkmarker(x, y, locationname) {
workmarker
=
new amap.marker({
map
:
map
,
title: locationname,
icon:
'http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png'
,
position: [x, y]
});
}
function loadworkrange(x, y, t, color, v) {
arrivalrange.search([x, y], t, function (status, result) {
if
(result.bounds) {
for
(var i
=
0
; i < result.bounds.length; i
+
+
) {
/
/
新建多边形对象
var polygon
=
new amap.polygon({
map
:
map
,
fillcolor: color,
fillopacity:
"0.4"
,
strokecolor: color,
strokeopacity:
"0.8"
,
strokeweight:
1
});
/
/
得到到达圈的多边形路径
polygon.setpath(result.bounds[i]);
polygonarray.push(polygon);
}
}
}, {
policy: v
});
}
function addmarkerbyaddress(address, money,imgurl) {
var geocoder
=
new amap.geocoder({
city:
"杭州"
,
radius:
1000
});
geocoder.getlocation(address, function (status, result) {
var iconvalue
=
"";
var _money
=
money;
if
(money.indexof(
"-"
) >
-
1
) {
_money
=
money.split(
"-"
)[
1
];
}
/
/
如果价格高于
3000
元
/
月在地图上显示红色,低于的话显示蓝色
if
(parsefloat(_money) >
3000
) {
iconvalue
=
"http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png"
;
}
else
{
iconvalue
=
"http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png"
;
}
if
(status
=
=
=
"complete"
&& result.info
=
=
=
'ok'
) {
var geocode
=
result.geocodes[
0
];
rentmarker
=
new amap.marker({
map
:
map
,
title: address,
icon:iconvalue,
animation:
"amap_animation_drop"
,
position: [geocode.location.getlng(), geocode.location.getlat()]
})
;
rentmarkerarray.push(rentmarker);
/
/
鼠标点击标记显示相应的内容
rentmarker.content
=
"<img src='"
+
imgurl
+
"'/><div>房源:<a target = '_blank' href='http://bj.58.com/pinpaigongyu/?key="
+
address
+
"'>"
+
address
+
"</a><p>价格:"
+
money
+
"</p><div>"
rentmarker.on(
'click'
, function (e) {
infowindow.setcontent(e.target.content);
infowindow.
open
(
map
, e.target.getposition());
if
(amaptransfer) amaptransfer.clear();
amaptransfer
=
new amap.transfer({
map
:
map
,
policy: amap.transferpolicy.least_time,
city:
"杭州市"
,
panel:
'transfer-panel'
});
amaptransfer.search([{
keyword: workaddress
}, {
keyword: address
}], function (status, result) {
})
});
}
})
}
function delworklocation() {
if
(polygonarray)
map
.remove(polygonarray);
if
(workmarker)
map
.remove(workmarker);
polygonarray
=
[];
}
function delrentlocation() {
if
(rentmarkerarray)
map
.remove(rentmarkerarray);
rentmarkerarray
=
[];
}
function loadworklocation() {
/
/
首先清空地图上已有的到达圈
delworklocation();
var geocoder
=
new amap.geocoder({
city:
"杭州"
,
radius:
1000
});
geocoder.getlocation(workaddress, function (status, result) {
if
(status
=
=
=
"complete"
&& result.info
=
=
=
'ok'
) {
var geocode
=
result.geocodes[
0
];
x
=
geocode.location.getlng();
y
=
geocode.location.getlat();
/
/
加载工作地点标记
loadworkmarker(x, y);
/
/
加载
60
分钟内工作地点到达圈
loadworkrange(x, y,
60
,
"#3f67a5"
, vehicle);
/
/
地图移动到工作地点的位置
map
.setzoomandcenter(
12
, [x, y]);
}
})
}
<
/
script>
<
/
body>
|
想要获取完整的代码github:https://github.com/divibear/pythondemo.git 新手上路,老司机们勿喷! 。
以上所述是小编给大家介绍的python爬虫租房信息在地图上显示的方法详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:https://blog.csdn.net/qq_36091581/article/details/76944053 。
最后此篇关于python爬虫租房信息在地图上显示的方法的文章就讲到这里了,如果你想了解更多关于python爬虫租房信息在地图上显示的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!