本次实验利用某爬虫软件爬取某房产网站中苏州地区的房产数据,数据文件为house.xlsx,部分数据显示如下。 house.png(1)如何利用Python从地址中提取出区的数据,从建造年代中提取出房龄数据
时间: 2024-02-19 16:57:41 浏览: 21
可以使用Python中的正则表达式库re来从地址中提取出区的数据,代码如下:
```python
import re
address = "江苏省苏州市相城区元和街道江南家园3幢1单元401室"
pattern = r"苏州市(.+?)区"
match = re.search(pattern, address)
if match:
district = match.group(1)
print(district)
```
该代码中的正则表达式`r"苏州市(.+?)区"`表示匹配以“苏州市”开头,以“区”结尾,中间部分为任意字符的字符串,并使用括号将区的名称部分作为捕获组。运行代码后,输出为“相城”。
从建造年代中提取房龄数据的方法也可以使用正则表达式,代码如下:
```python
import re
build_year = "2005年"
pattern = r"(\d{4})年"
match = re.search(pattern, build_year)
if match:
year = int(match.group(1))
age = 2021 - year
print(age)
```
该代码中的正则表达式`r"(\d{4})年"`表示匹配以四个数字和“年”结尾的字符串,并使用括号将四个数字作为捕获组。运行代码后,输出为“16”,表示该房屋的房龄为16年。
相关问题
python爬取链家二手房信息400条,并存入house.xlsx表格中
好的,我可以为您提供以下代码来实现这个爬取任务。不过需要注意的是,网站的反爬虫机制比较严格,如果爬取过于频繁会被封IP,建议加上一些延时等措施。
```
import requests
import openpyxl
from bs4 import BeautifulSoup
import time
url = "https://bj.lianjia.com/ershoufang/pg"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = "house"
sheet['A1'] = '小区名称'
sheet['B1'] = '户型'
sheet['C1'] = '面积'
sheet['D1'] = '朝向'
sheet['E1'] = '楼层'
sheet['F1'] = '年代'
sheet['G1'] = '总价(万)'
sheet['H1'] = '单价(元/平米)'
row = 2
for i in range(1, 21):
print("正在爬取第{}页数据".format(i))
res = requests.get(url + str(i), headers=headers)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
house_list = soup.find_all('div', {'class': 'info clear'})
for house in house_list:
name = house.find('div', {'class': 'title'}).a.get_text()
room = house.find('div', {'class': 'houseInfo'}).get_text().split('|')[1].strip()
square = house.find('div', {'class': 'houseInfo'}).get_text().split('|')[2].strip()
direction = house.find('div', {'class': 'houseInfo'}).get_text().split('|')[3].strip()
floor = house.find('div', {'class': 'positionInfo'}).get_text().split('-')[1].strip()
year = house.find('div', {'class': 'positionInfo'}).get_text().split('-')[0].strip()
total_price = house.find('div', {'class': 'totalPrice'}).span.get_text()
unit_price = house.find('div', {'class': 'unitPrice'}).get_text().strip()[2:-4]
sheet.cell(row=row, column=1, value=name)
sheet.cell(row=row, column=2, value=room)
sheet.cell(row=row, column=3, value=square)
sheet.cell(row=row, column=4, value=direction)
sheet.cell(row=row, column=5, value=floor)
sheet.cell(row=row, column=6, value=year)
sheet.cell(row=row, column=7, value=total_price)
sheet.cell(row=row, column=8, value=unit_price)
row += 1
time.sleep(1)
wb.save('house.xlsx')
print("数据爬取完成!")
```
python爬虫--房产数据爬取并保存本地
好的,我可以为您介绍一下如何使用 Python 爬虫来爬取房产数据并保存到本地。
首先,您需要选择一个合适的网站来进行爬取。例如,可以选择链家网、安居客等房产网站。然后,您需要分析该网站的页面结构和数据请求方式,确定需要爬取哪些数据,以及如何获取这些数据。
接着,您可以使用 Python 的 requests 库来发送 HTTP 请求,获取网页内容。然后,可以使用 BeautifulSoup 库来解析网页内容,提取需要的数据。
最后,您可以使用 Python 的 Pandas 库将数据保存到本地的 Excel 或 CSV 文件中。以下是一个示例代码:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 发送 HTTP 请求获取网页内容
url = 'https://bj.lianjia.com/ershoufang/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
# 解析网页内容,提取需要的数据
house_list = soup.find_all('div', {'class': 'info'})
data = []
for house in house_list:
name = house.find('div', {'class': 'title'}).text.strip()
price = house.find('div', {'class': 'price'}).text.strip()
area = house.find('div', {'class': 'houseInfo'}).text.strip()
data.append([name, price, area])
# 将数据保存到本地文件
df = pd.DataFrame(data, columns=['名称', '价格', '面积'])
df.to_excel('house_data.xlsx', index=False)
```
这是一个简单的示例代码,您可以根据您需要爬取的数据和网站的不同来进行修改和优化。