-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawlerApplication for cities.py
242 lines (192 loc) · 6.93 KB
/
CrawlerApplication for cities.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import urllib.request
import lxml.etree
from bs4 import BeautifulSoup
import re
import datetime
import time
import csv
import os
ITEMS_PAGE = 30
TIME_SLEEP = 5
API = "a3cde9e1462bb4d112bae6620ea9ac92"
CITY = ['binzhou','liaocheng']
CITY_NAME = ['滨州','聊城']
class Spider:
def getContent(self, url):
conn = urllib.request.urlopen(url)
output = lxml.etree.HTML(conn.read())
return output
def saveContent(self, filePath, content):
file_obj = open(filePath, 'w', encoding = 'UTF-8')
file_obj.write(content)
file_obj.close()
class Community:
communtyPrice = ''
communityLat = ''
communityLng = ''
communityName = ''
communityAdd = ''
communityTotalArea = ''
communityTotalFamily = ''
communityBuildYear = ''
communityGreenRate = ''
def getResultFromJS(destStr):
stringFind = str( soup.findAll(text = re.compile(destStr)) )
#print(stringFind)
strBegin = stringFind.find(destStr)
#print("strBegin:", strBegin)
count = 0
posBegin = 0
posEnd = 0
for i in range(strBegin, len(stringFind)):
if (stringFind[i] == '\''):
count += 1
if (count == 2):
posBegin = i + 1
if (count == 3):
posEnd = i
break
#print(posBegin)
#print(posEnd)
resultStr = stringFind[posBegin: posEnd]
#print(resultStr)
return resultStr
def getPrice(destStr):
stringFind = str( soup.select("em[class=" + destStr + "]") )
#print(stringFind)
posBegin = stringFind.find(">")
posEnd = stringFind.find("</em>")
for i in range(posBegin, len(stringFind)):
if (stringFind[i].isdigit()):
posBegin = i
break
price = stringFind[posBegin : posEnd]
print(price)
return price
def getAddress(destStr):
stringFind = str( soup.select("dd[class=" + destStr + "]") )
#print(stringFind)
posBegin = stringFind.find("<em>")
posEnd = stringFind.find("</em>")
address = stringFind[posBegin + 4 : posEnd]
#print(address)
return address
def getBasicInformation(className, destStr):
stringFind = str( soup.find_all('dl',class_ = className) )
#print(stringFind)
stringFind = stringFind[stringFind.find(destStr):]
posBegin = stringFind.find("<dd>")
posEnd = stringFind.find("</dd>")
info = stringFind[posBegin + 4 : posEnd]
#print(info)
return info
def getCommunityLink(html):
soup = BeautifulSoup(html, "lxml")
stringFind = str( soup.findAll(href = re.compile('community/view')) )
#print(stringFind)
#print("start")
numList = [0]
while ( len(stringFind) > 3):
posBegin = stringFind.find('community/view/') + len('community/view/')
stringFind = stringFind[posBegin:]
posEnd = stringFind.find('/')
num = stringFind[0 : posEnd]
stringFind = stringFind[posEnd:]
#print(posBegin)
#print(posEnd)
#print(num)
if (num != numList[len(numList) - 1]) and num.isdigit():
numList.append(num)
del numList[0]
# print(len(numList))
# print(numList)
for i in range(0 , len(numList)):
numList[i] = "http://" + CITY[ITEM_CITY] + ".anjuke.com/community/view/" + numList[i]
print(numList)
return numList
def getCommunityAmount(html, className, destStr):
soup = BeautifulSoup(html, "lxml")
stringFind = str( soup.find_all(class_ = className) )
#print(stringFind)
posBegin = 0
posEnd = 0
for i in range(0 , len(stringFind)):
if stringFind[i].isdigit():
posBegin = i
break
i = len(stringFind) - 1
for i in range(len(stringFind) - 1, 0, -1):
if stringFind[i].isdigit():
posEnd = i
break
info = stringFind[posBegin : posEnd + 1]
print(info)
return int(info)
def writeToFile(dataContent, method):
with open('./data/' + CITY[ITEM_CITY] + '.csv', method, encoding = 'utf-8-sig',newline='') as f:
writer = csv.writer(f, delimiter = ',')
writer.writerow(dataContent)
f.close()
totalPage = 1
ID = 1
dataOutput = []
headName = ['ID', '小区名', '地址', '经度', '纬度', '均价(元/平米)', '总住户', '总面积', '绿化率', '建造年代', 'LINK']
#dataOutput.append(headName)
#spider = Spider()
print("Start.......")
timeBegin = datetime.datetime.now()
#Find total pages
#print("test")
#os.remove("/data/" + CITY[0] + ".csv")
ITEM_CITY = 0
for ITEM_CITY in range(0,len(CITY)):
ID = 1
url = "http://" + CITY[ITEM_CITY] + ".anjuke.com/community/p"
writeToFile(headName,"w")
totalNumHTML = urllib.request.urlopen("http://" + CITY[ITEM_CITY] + ".anjuke.com/community/view").read()
totalPage = int(getCommunityAmount(totalNumHTML, "tit", "") / ITEMS_PAGE) + 1
print("Total pages: " + str(totalPage))
for pageNumber in range(1, totalPage + 1):
currentURL = url + str(pageNumber)
print("View page:", pageNumber, " site:", currentURL)
result = []
communityList = getCommunityLink(urllib.request.urlopen(currentURL).read())
data = [0]
data[0] = ID
for i in range(0, len(communityList)):
print("#########################################################################")
print("No.", ID)
communityLink = urllib.request.urlopen(communityList[i]).read()
soup = BeautifulSoup(communityLink, "lxml")
data.append(getResultFromJS("comm_name"))
data.append(CITY_NAME[ITEM_CITY] + getAddress("comm-adres"))
data.append(getResultFromJS("comm_lng"))
data.append(getResultFromJS("comm_lat"))
data.append(getPrice("comm-avg-price"))
data.append(getBasicInformation("comm-r-detail float-r", "总户数"))
data.append(getBasicInformation("comm-r-detail float-r", "总建面"))
data.append(getBasicInformation("comm-r-detail float-r", "绿化率"))
data.append(getBasicInformation("comm-r-detail float-r", "建造年代"))
data.append(communityList[i])
ID += 1
#dataOutput += data
writeToFile(data,"a")
data.clear()
data.append(ID)
time.sleep(TIME_SLEEP)
# if ID == 3:
# break
print("Start to write")
# with open('./data/' + CITY[0] + '.csv', 'w', encoding = 'utf-8-sig') as f:
# writer = csv.writer(f, delimiter = ',')
# writer.writerow(headName)
# length = len(dataOutput)
# for i in range(0, len(dataOutput),len(headName)):
# writer.writerow(dataOutput[i : i + len(headName)])
#
# f.close()
timeEnd = datetime.datetime.now()
print("Time: " + str((timeEnd - timeBegin).seconds) + "s")
print("Success!")
#CITY = ["fangchenggang",'','','','','','','','','','','','']
#CITY_NAME = ['防城港市','济南市','青岛市','德州市','淄博市','潍坊市','烟台市','日照市','德州市','','']