桥西区二手房出租爬虫信息
时间: 2023-12-17 16:28:08 浏览: 26
以下是一个简单的Python爬虫程序,可以爬取链家网站桥西区二手房出租信息并将其保存到本地文件中:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://lf.lianjia.com/zufang/qiaoxiqu/pg1/'
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.text, 'html.parser')
house_list = soup.find_all('div', class_='content__list--item--main')
with open('qiaoxi_rent.txt', 'w', encoding='utf-8') as f:
for house in house_list:
title = house.find('p', class_='content__list--item--title').text.strip()
price = house.find('span', class_='content__list--item-price').text.strip()
f.write(title + ' ' + price + '\n')
```
该程序使用requests库和BeautifulSoup库来获取和解析网页内容。程序首先定义了要爬取的网页URL和请求头部信息,然后使用requests库发送GET请求获取网页内容。接着使用BeautifulSoup库解析网页内容,找到所有房源信息所在的HTML标签,并逐一提取每个房源的标题和价格信息。最后将提取到的信息保存到本地文件中。