requets相关练习

requets相关练习

爬取贝壳租房并将房屋信息写入excel文件

import requests
from re import *
import openpyxl

# # requests.get(网页地址)        -       获取指定页面的数据返回一个响应对象
response = requests.get('https://cd.zu.ke.com/zufang')

# 获取响应的状态码
# print(response.status_code)
if response.status_code == 200:
    pass

# 从网页源代码中匹配所有的租房名字
result = findall(r'title="[\u4e00-\u9fa5]租·\w+',response.text)
new_result = findall(r'[\u4e00-\u9fa5]租·\w+', str(result))

# 从源代码中匹配所有的租房价格
fir_price = findall(r'\d+</em>\s元/月', response.text)
price = findall(r'\d+', str(fir_price))

# 创建工作表对象
try:
    workbook = openpyxl.open('file\house_price.xlsx')
except FileNotFoundError:
    workbook = openpyxl.Workbook()
    workbook.save('file\house_price.xlsx')

# 创建工作表对象
if 'house_price' in workbook.sheetnames:
    sheet = workbook['house_price']
else:
    sheet = workbook.create_sheet('house_price')
    workbook.save('file\house_price.xlsx')

# 单元格写操作
for row in range(1, len(new_result)):
    sheet.cell(row, 1).value = new_result[row-1]
    sheet.cell(row, 2).value = price[row-1] + '元/月'
workbook.save('file\house_price.xlsx')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值