用Python写一个python龙港房地产爬虫
时间: 2024-04-12 19:20:23 浏览: 62
好的,以下是一个简单的Python爬虫,用于抓取龙港房地产网站的房源信息:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.lgfdc.com.cn/property/searchList'
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'}
# 获取总页数
res = requests.post(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
total_page = int(soup.find('input', {'id': 'total'}).get('value'))
# 遍历每一页,抓取房源信息
for page in range(1, total_page + 1):
data = {
'page': str(page),
'sort': 'default',
'priceRange': '',
'buildingType': '',
'houseType': '',
'feature': '',
'developer': '',
'district': '',
'region': '',
'price': '',
'area': '',
'age': '',
'floor': '',
'orientation': '',
'decorate': '',
'propertyRight': '',
'propertyType': '',
'other': ''
}
res = requests.post(url, headers=headers, data=data)
soup = BeautifulSoup(res.text, 'html.parser')
items = soup.find_all('div', {'class': 'info'})
for item in items:
name = item.find('a', {'class': 'name'}).text.strip()
price = item.find('span', {'class': 'price'}).text.strip()
area = item.find('span', {'class': 'area'}).text.strip()
print(name, price, area)
```
以上代码会抓取龙港房地产网站的所有房源信息,并输出房源名称、价格和面积。您可以根据需求修改代码以满足您的实际需求。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)