import requests
from bs4 import BeautifulSoup
from re import *
import csv
def looking(pages1):
response = requests.get(f'https://cd.zu.ke.com/zufang/pg{pages1}#contentList')
'''
https://cd.zu.ke.com/zufang/pg1#contentList
https://cd.zu.ke.com/zufang/pg2/#contentList
https://cd.zu.ke.com/zufang/pg3/#contentList
'''
soup = BeautifulSoup(response.text, 'lxml')
all_house = soup.select('.content__list>.content__list--item')
data = []
for house in all_house:
name = house.select_one('.twoline').text.strip()
name1 = name.split()
name2 = name1[0]
price = house.select_one('.content__list--item-price').text
p = house.select_one('.content__list--item--des')
message = sub(r'\s+', '', p.text)
list = message.split('/')
location = list[-5]
area = list[-4]
model = list[-2]
data.append([name2, price, location, area, model])
writer = csv.writer(open('files/租房.csv', "a", newline=''))
if pages1 == 0:
writer.writerow(['名字', '价格', '位置', '面积', '户型'])
writer.writerows(data)
for pages1 in range(100):
looking(pages1)
import time
time.sleep(1)
|