forked from dengyings/biji
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoleju.py
More file actions
279 lines (246 loc) · 12.6 KB
/
Doleju.py
File metadata and controls
279 lines (246 loc) · 12.6 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 9 21:04:41 2019
@author: tarena
"""
from selenium import webdriver
import json
import requests
class Doleju(object):
def __init__(self,name,posswd,firename):
self.name = name
self.posswd = posswd
self.firename = firename
self.s = requests.session()
logger = logging.getLogger("AJK")
# 指定logger的输出格式
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
# 文件日志,终端日志
file_handler = logging.FileHandler("AJK.txt")
file_handler.setFormatter(formatter)
consle_handler = logging.StreamHandler(sys.stdout)
consle_handler.setFormatter(formatter)
# 设置默认的级别
logger.setLevel(logging.INFO)
# 把文件日志和终端日志添加到日志处理器中
logger.addHandler(file_handler)
logger.addHandler(consle_handler)
def login(self):
driver = webdriver.Firefox(executable_path='/home/tarena/anaconda3/geckodriver')
driver.get("http://j.esf.leju.com/ucenter/login?curcity=suzhou")
time.sleep(2)
driver.find_element_by_id('username').click()
driver.find_element_by_id('username').clear()
driver.find_element_by_id('username').send_keys(self.name)
driver.find_element_by_id("password").click()
driver.find_element_by_id('password').clear()
driver.find_element_by_id("password").send_keys(self.posswd)
driver.find_element_by_id("btn_login").click()#登录摁扭
time.sleep(2)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
driver.find_element_by_xpath("/html/body/div[5]/div[2]/div").click()#关闭弹窗
except:
pass
driver.find_element_by_xpath('//div[@id="scrollList"]/dl[9]/dd[1]/a').click()
def begin(self):
hothouse = self.Lowerhouse()
html = driver.page_source
Num = re.findall(r'sj_number pa[\s\S]*?<span>([\s\S]*?)</span>',html)[0]
if Num == 0:
pass
else:
self.Release(Num,hothouse)#发布房源
self.writeinfo(infos)
self.writedays(infos)
def writedays(self,infos):
infos = self.showinfo()
self.writeinfo(infos)
L = []
for info in infos:
if int(item["累计点击"]) >= 5:
day = 30 - int(item["发布天数"])
else:
day = 15 - int(item["发布天数"])
L.append(day)
mintime = min(L)
with open('/home/tarena/DYProject/Table/startdays.json', "ab") as f:
text = json.dumps(dict(info),ensure_ascii=False)+'\n'
f.write(text.encode('utf-8'))
def Release(self,Num,hothouse):
if len(hothouse) == int(Num):
for firename in hothouse:
self.doleju(firename)
elif len(hothouse) < int(Num):
infos = self.showinfo()#已发布房源信息
Name = []#已发布房源信息
house = []#用户房源列表
for info in infos:
L = ([info["xiaoqu"],info["mianji"]]])
res = "_".join(L)
Name.append(res)
a = int(Num) - len(hothouse)
with open("/home/tarena/myproject/myproject/DYProject/user/{}/house.txt".format(self.name),r) as f:
text = f.readlines()
for i in text:
if i not in Name and i not in hothouse:
house.append(i)
Newlist = random.sample(house, a)
for firename in Newlist:
self.doleju(firename)
else:
pass
def doleju(self,firename):
resttime = 60#图片上传时间
self.verify_firename(firename)
driver.find_element_by_xpath('//div[@id="scrollList"]/dl[9]/dd[1]/a').click()#出售房源
driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/div/div/div[1]/a[1]/span").click()#发布房源
driver.find_element_by_id("commName").click()
driver.find_element_by_id('commName').clear()
driver.find_element_by_id("commName").send_keys(info["xiaoqu"])
time.sleep(10)
driver.find_element_by_xpath('//div[@class="drop-box dw_352"]/ul/li/span').click()
driver.find_element_by_id('tPrice').click()
driver.find_element_by_id('tPrice').send_keys('520')
driver.find_element_by_xpath('//div[@class="fill mb20"]/i').click()
driver.find_element_by_id('s').send_keys(info["hushi"])
driver.find_element_by_id('t').send_keys(info["ting"])
driver.find_element_by_id('w').send_keys(info["wei"])
driver.find_element_by_id('area').send_keys(info["mianji"])
driver.find_element_by_id('selectFloor').click()
driver.find_element_by_id("ptzz").click()
driver.find_element_by_xpath('//select[@id="ptzz"]/option[1]').click()
driver.find_element_by_id('zsqk').click()#装修情况
driver.find_element_by_xpath('//select[@id="zsqk"]/option[6]').click()
driver.find_element_by_id('sx').click()
driver.find_element_by_xpath('//select[@id="sx"]/option[5]').click()
driver.find_element_by_id('selectFloor').click()
driver.find_element_by_xpath('//select[@id="selectFloor"]/option[4]').click()
driver.find_element_by_id('totalfloor').send_keys(info["zonggao"])
driver.find_element_by_id('buildyear').click()
driver.find_element_by_id('buildyear').clear()
driver.find_element_by_id('buildyear').send_keys(info["niandai"])
driver.find_element_by_xpath('//select[@name="property_rights"]').click()
driver.find_element_by_xpath('//select[@name="property_rights"]/option[2]').click()
driver.find_element_by_id('tag_24').click()
driver.find_element_by_id('tag_21').click()
driver.find_element_by_id('tag_19').click()
driver.find_element_by_id('htit').send_keys(info["title"])
driver.find_element_by_xpath('//table[@class="ke-textarea-table"]/tbody/tr/td/iframe').send_keys(info["fangyuanxinxi"])
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/1.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/2.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/3.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/4.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/5.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/6.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/7.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/8.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_sn"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/9.jpg")
time.sleep(resttime)
driver.find_element_by_xpath('//div[@id="upload_fx"]/div[2]/input').send_keys("/home/tarena/myproject/myproject/picture/10.jpg")
time.sleep(resttime)
driver.find_element_by_id('xqdatabase').click()
try:
driver.find_element_by_xpath('//div[@class="choosePic"]/ul/li[1]/div[2]').click()
driver.find_element_by_xpath('//div[@class="choosePic"]/ul/li[2]/div[2]').click()
driver.find_element_by_xpath('//div[@class="choosePic"]/ul/li[3]/div[2]').click()
except:
pass
driver.find_element_by_id('pubBtn').click()
driver.find_element_by_xpath('//button[@class="ui-dialog-autofocus"').click()
def verify_firename(self, firename):
with open("/home/tarena/myproject/myproject/DYProject/houseinfo/houseinfo.json",'r', encoding='utf-8') as f:
load_dict = json.load(f)
if firename in load_dict.keys():
info = load_dict[firename]
return info
else:
Leju.AutoCopy(firename)
info = load_dict[firename]
return info
def showinfo(self):
page = re.findall(r'data-page="4">(\d)</a><a href="javascript:;" class="next"', html1)[0]
for i in range(1,int(page)):
time.sleep(1)
number = re.findall(r'data-unityinfoid="(\d+)"', esc)[0]
days = re.findall(r'<span class="left-time">[\u4e00-\u9fa5]{2}.?(\d+)[\u4e00-\u9fa5]{1}', esc)[0]
everchick = re.findall(r'<p class="click-detail\d+">(\d+)/(\d+)</p>', esc)[0][0]
newchick = re.findall(r'<p class="click-detail\d+">(\d+)/(\d+)</p>', esc)[0][1]
chicks = int(everchick) + int(newchick)
title = re.findall(r'>([\s\S]*?)</span>', info)[0]
acreage = re.findall(r'</span>.?([\s\S]*?[\u4e00-\u9fa5])[\s\S]*?</p>', info)[0]
price = re.findall(r'span>[\s\S]*?(\d+.?\d{2}[\u4e00-\u9fa5]{1})</p>', info)[0]
item["房源编号"]=number
item["发布天数"]=days
item["是否发布"]=fb
item["累计点击"]=chicks
item["小区"]=title
item["面积"]=acreage
item["价格"]=price
ALLINFO.append(item)
try:
driver.find_element_by_class_name("next").click()
except:
pass
self.writeinfo(ALLINFO)
return ALLINFO
def writeinfo(self,ALLINFO):
fout = codecs.open('/home/tarena/myproject/myproject/DYProject/user/{}/show.html'.format(self.name),'w',encoding='utf-8')
fout.write("<html>")
fout.write("<head><meta charset='utf-8'/><head>")
fout.write("<body>")
fout.write("<table>")
for info in ALLINFO:
fout.write("<tr>")
fout.write("<td>%s</td>"%info["小区"])
fout.write("<td>%s</td>"%info["面积"])
fout.write("<td>%s</td>"%info["价格"])
fout.write("<td>%s</td>"%info["累计点击"])
fout.write("</table>")
fout.write("</body>")
fout.write("</html>")
fout.close()
def __Change(self):
infos = self.showinfo()
hothouse = []
Number = []
for info in infos:
if int(item["累计点击"]) > 30:
L = ([info["xiaoqu"],info["mianji"]])
res = "_".join(L)
hothouse.append(res)
if int(item["发布天数"]) >= 15 and int(item["累计点击"]) < 5:
Number.append(item["房源编号"])
if int(item["发布天数"]) > 30:
Number.append(item["房源编号"])
return hothouse, Number
def Lowerhouse(self):#下架房源
html = driver.page_source
hothouse, Number = self.__Change()
pages = re.findall(r'agent_page clearfix[\s\S]*?<span>([\s\S]*?)</span>',html)[0]
page = re.findall(r'\d+')[1]#页数
numb = re.findall(r'\d+')[0]#房源数量
driver.find_element_by_xpath('//div[@class="choosePic"]/ul/li[last()]').click()#尾页
for i in range(int(page)-1):
self.__low(Number)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")#下拉到底部
time.sleep(0.5)
driver.find_element_by_xpath('//div[@class="choosePic"]/ul/li[2]').click()#上一页
return hothouse
def __low(self,Number):
for num in Number:
try:
driver.find_element_by_xpath('//div[@data-id="{}"]/div[1]/div[1]\div[1]\div[2]\i]'.format(num)).click()
except:
pass
driver.find_element_by_xpath('//div[@class="sec-2 clearfix"]/div[1]/a[1]').click()
driver.find_element_by_xpath('//button[@class="ui-dialog-autofocus"]').click()#确定