from pyquery import PyQuery as pq
import requests
from selenium import webdriver
import re
import csv
driver = webdriver.Edge()
driver.get('https://www.ziroom.com/z/')
html2 = driver.page_source
html = pq(html2)
div = html('.Z_list-box').children()
print(html('.title'))
print(type(div.find('h5').text()))
title = [x for x in div.find('h5').text().split(" ")]
#print(len(title))
decs = [y for y in div.find('.desc').text().split(' ')]
square = [decs[i] for i in range(0,len(decs),3)]
#print(len(square))
place = [ decs[i] for i in range(2,len(decs),3)]
#print(len(decs))
price = [s for x in div.find('price').text()]
geju = re.findall(r'\s+(.*?)\s+(.*?)\s+(.*?)\s+
',html2)
#print(len(geju))
with open("租房信息.csv","w",newline="",encoding="utf-8") as f:
header = ["标题","平米数","层高","位置","价格","布局"]
f_csv = csv.DictWriter(f,fieldnames=header)
f_csv.writeheader()
for i in range(len(title)):
f_csv.writerow({
"标题":title[i],
"平米数":square[i],
"层高":place[i],
"价格":[]
# ,"布局":geju[i]
})
print("over!!!")