day19-homework
from re import findall
import openpyxl
import os
import requests
res = requests.get(
url='https://cd.zu.ke.com/zufang',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
)
a = res.text
area = findall(r'<a\s+target="_blank"\s+href="/zufang/\S+>([\u4e00-\u9fa5]+)</a>', a)
sm_area = findall(r'target="_blank">([\u4e00-\u9fa5]+)</a>-', a)
community = findall(r'<a\s+title="(\S+)"', a)
money = findall(r'<em>(\d+)</em>', a)
address = []
money_ = []
for i in range(len(area)):
address.append(f'{area[i]}-{sm_area[i]}-{community[i]}')
money_.append(f'¥{money[i]}')
print(address,money_)
if os.path.exists('抓取结果.xlsx'):
workbook = openpyxl.load_workbook('抓取结果.xlsx')
else:
workbook = openpyxl.Workbook()
if '住房信息' in workbook.sheetnames:
sheet = workbook['住房信息']
else:
sheet = workbook.create_sheet('住房信息')
m_row = sheet.max_row
m_col = sheet.max_column
for row in range(1, m_row + 1):
for col in range(1, m_col + 1):
sheet.cell(row, col).value = None
current_row = 1
sheet.cell(current_row, 1).value = '地址'
sheet.cell(current_row, 2).value = '价格'
current_row += 1
for add in address:
sheet.cell(current_row, 1).value = add
current_row += 1
current_row = 2
for mon in money_:
sheet.cell(current_row, 2).value = mon
current_row += 1
workbook.save('抓取结果.xlsx')