python 安居客 爬虫_Python爬虫安居客房价信息(并利用百度地图API查询坐标)
时间: 2023-12-19 15:22:13 浏览: 133
好的,以下是爬取安居客房价信息并利用百度地图API查询坐标的Python爬虫代码:
```python
import requests
from bs4 import BeautifulSoup
import json
def get_location(city, address):
url = 'http://api.map.baidu.com/geocoder/v2/'
params = {
'address': address,
'city': city,
'output': 'json',
'ak': '你的百度地图API密钥'
}
res = requests.get(url, params=params)
json_data = json.loads(res.text)
if json_data['status'] == 0:
location = json_data['result']['location']
return location['lng'], location['lat']
def get_house_price(city, area):
url = 'https://{0}.anjuke.com/market/{1}/'.format(city, area)
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=url, headers=headers)
soup = BeautifulSoup(res.text, 'lxml')
house_price_list = []
for tr in soup.select('.sale-estate-table tr')[1:]:
tds = tr.select('td')
house_name = tds[0].text.strip()
house_address = tds[1].text.strip()
house_price = tds[2].text.strip()
house_location = get_location(city, house_address)
if house_location:
house_price_list.append({
'name': house_name,
'address': house_address,
'price': house_price,
'location': house_location
})
return house_price_list
if __name__ == '__main__':
city = 'sh'
area = 'pudongxinqu'
house_price_list = get_house_price(city, area)
print(house_price_list)
```
其中,`get_location`函数用于利用百度地图API查询地址的坐标,`get_house_price`函数用于爬取安居客网站上的房价信息。在`if __name__ == '__main__'`中,我们可以指定城市和区域,通过调用`get_house_price`函数来获取该区域内的房价信息,并将结果打印出来。
注意:在使用本代码前请替换百度地图API密钥。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)