写一段python代码爬取58同城中北京地区的新房房屋信息,包括标题,面积,均价,总价,标签,用途,户型,装修情况,朝向等信息,并存储为csv文件
时间: 2023-06-04 22:05:23 浏览: 154
好的,以下是可以回答的Python代码:
```python
import requests
from bs4 import BeautifulSoup
import csv
url = "https://bj.58.com/ershoufang/"
params = {
"PGTID": "0d100000-0000-18d3-3a3b-6c133d6d2fbc",
"ClickID": 1
}
res = requests.get(url, params=params)
res.encoding = "utf-8"
soup = BeautifulSoup(res.text, "html.parser")
with open("house_info.csv", "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["标题", "面积", "均价", "总价", "标签", "用途", "户型", "装修情况", "朝向"])
for house in soup.select(".list > li"):
title = house.select_one(".title > a").string.strip()
area = house.select_one(".area > div > div:first-child").string.strip()
unit_price = house.select_one(".unit > span:first-child").string.strip()
total_price = house.select_one(".total > span").string.strip()
tags = "/".join([tag.string.strip() for tag in house.select(".baseinfo > div:first-child > a")])
usage = house.select_one(".baseinfo > div:first-child > span").string.strip()
layout = house.select_one(".baseinfo > div:nth-child(2) > span:nth-child(1)").string.strip()
decoration = house.select_one(".baseinfo > div:nth-child(2) > span:nth-child(2)").string.strip()
orientation = house.select_one(".baseinfo > div:nth-child(2) > span:nth-child(3)").string.strip()
writer.writerow([title, area, unit_price, total_price, tags, usage, layout, decoration, orientation])
print("数据已存储为 house_info.csv 文件。")
```
注意:此代码的运行结果仅供参考,使用前需仿照运行环境和数据结构进行调整。
阅读全文