房天下爬虫代码爬重庆二手房
时间: 2023-12-17 17:27:56 浏览: 45
以下是使用Python的requests和BeautifulSoup库编写的房天下爬虫代码,可以爬取重庆的二手房信息:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://cq.esf.fang.com/'
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.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
house_list = soup.select('.shop_list.shop_list_4 > dl')
for house in house_list:
title = house.select('dd > p:nth-of-type(1) > a')[0].text.strip()
price = house.select('dd > div > span:nth-of-type(1)')[0].text.strip()
area = house.select('dd > div > span:nth-of-type(2)')[0].text.strip()
print(title, price, area)
```
该代码首先使用requests库获取重庆二手房的网页源代码,然后使用BeautifulSoup库解析网页源代码,最后从中提取出房屋的标题、价格和面积等信息,并打印输出。