记一次python 爬虫爬取深圳租房信息的过程及遇到的问题
作者:不迁徙候鸟 时间:2022-03-22 02:09:02
为了分析深圳市所有长租、短租公寓的信息,爬取了某租房公寓网站上深圳区域所有在租公寓信息,以下记录了爬取过程以及爬取过程中遇到的问题:
爬取代码:
import requests
from requests.exceptions import RequestException
from pyquery import PyQuery as pq
from bs4 import BeautifulSoup
import pymongo
from config import *
from multiprocessing import Pool
client = pymongo.MongoClient(MONGO_URL) # 申明连接对象
db = client[MONGO_DB] # 申明数据库
def get_one_page_html(url): # 获取网站每一页的html
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/85.0.4183.121 Safari/537.36"
}
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
else:
return None
except RequestException:
return None
def get_room_url(html): # 获取当前页面上所有room_info的url
doc = pq(html)
room_urls = doc('.r_lbx .r_lbx_cen .r_lbx_cena a').items()
return room_urls
def parser_room_page(room_html):
soup = BeautifulSoup(room_html, 'lxml')
title = soup.h1.text
price = soup.find('div', {'class': 'room-price-sale'}).text[:-3]
x = soup.find_all('div', {'class': 'room-list'})
area = x[0].text[7:-11] # 面积
bianhao = x[1].text[4:]
house_type = x[2].text.strip()[3:7] # 户型
floor = x[5].text[4:-2] # 楼层
location1 = x[6].find_all('a')[0].text # 分区
location2 = x[6].find_all('a')[1].text
location3 = x[6].find_all('a')[2].text
subway = x[7].text[4:]
addition = soup.find_all('div', {'class': 'room-title'})[0].text
yield {
'title': title,
'price': price,
'area': area,
'bianhao': bianhao,
'house_type': house_type,
'floor': floor,
'location1': location1,
'location2': location2,
'location3': location3,
'subway': subway,
'addition': addition
}
def save_to_mongo(result):
if db[MONGO_TABLE].insert_one(result):
print('存储到mongodb成功', result)
return True
return False
def main(page):
url = 'http://www.xxxxx.com/room/sz?page=' + str(page) # url就不粘啦,嘻嘻
html = get_one_page_html(url)
room_urls = get_room_url(html)
for room_url in room_urls:
room_url_href = room_url.attr('href')
room_html = get_one_page_html(room_url_href)
if room_html is None: # 非常重要,否则room_html为None时会报错
pass
else:
results = parser_room_page(room_html)
for result in results:
save_to_mongo(result)
if __name__ == '__main__':
pool = Pool() # 使用多进程提高爬取效率
pool.map(main, [i for i in range(1, 258)])
在写爬取代码过程中遇到了两个问题:
(一)在get_room_url(html)函数中,开始是想直接return每个租房信息的room_url,但是return不同于print,函数运行到return时就会结束该函数,这样就只能返回每页第一个租房room_url。解决办法是:return 包含每页所有room_url的generator生成器,在main函数中用for循环遍历,再从每个room_url中获取href,传入到get_one_page_html(room_url_href)中进行解析。
(二)没有写第76行的if语句,我默认get_one_page_html(room_url_href)返回的room_html不为空,因此出现multiprocessing.pool.RemoteTraceback报错:
上图中显示markup为None情况下报错,点击蓝色"F:\ProgramFiles\anaconda3\lib\site-packages\bs4\__init__.py"发现markup为room_html,即部分room_html出现None情况。要解决这个问题,必须让代码跳过room_html is None的情况,因此添加 if 语句解决了这个问题。
最终成功爬取某租房公寓深圳市258页共4755条租房信息,为下一步进行数据分析做准备。
其中单条信息:
来源:https://www.cnblogs.com/chang2021/p/14021768.html
标签:python,爬虫,租房信息,深圳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP中一个字符串处理类加强版
2008-11-25 18:07:00
python使用梯度下降和牛顿法寻找Rosenbrock函数最小值实例
2022-09-10 20:01:20
![](https://img.aspxhome.com/file/2023/7/101447_0s.jpg)
Python深度学习之使用Albumentations对图像做增强
2023-11-24 06:18:23
![](https://img.aspxhome.com/file/2023/5/64275_0s.jpg)
python安装pillow的三种方法
2023-07-20 02:36:53
![](https://img.aspxhome.com/file/2023/5/79495_0s.png)
如何在conda虚拟环境中配置cuda+cudnn+pytorch深度学习环境
2022-06-16 14:43:04
![](https://img.aspxhome.com/file/2023/6/107036_0s.png)
python的数据与matlab互通问题:SciPy
2022-05-16 04:53:48
如何利用SQL Server 2005中的模板参数
2009-01-23 15:02:00
Linux CentOS Python开发环境搭建教程
2021-05-17 22:57:18
python3中的logging记录日志实现过程及封装成类的操作
2023-07-30 21:58:21
解读python基于netconf协议获取网元的数据
2023-06-29 20:26:23
![](https://img.aspxhome.com/file/2023/7/60297_0s.jpg)
python中的zip模块
2023-09-13 07:49:56
交互设计师的尴尬-我的强项是什么?
2008-12-29 14:00:00
界面内容优化的层次
2007-11-06 13:07:00
判定IE的各个版本
2010-01-19 14:01:00
phpmyadmin报错:#2003 无法登录 MySQL服务器的解决方法
2023-11-19 20:38:45
如何使用pycharm连接Databricks的步骤详解
2023-01-28 23:19:03
![](https://img.aspxhome.com/file/2023/3/72983_0s.png)
详解pandas获取Dataframe元素值的几种方法
2022-12-28 07:30:01
Python多线程爬虫实战_爬取糗事百科段子的实例
2021-02-15 16:58:21
Python利用PaddleOCR制作个搜题小工具
2022-07-22 16:18:18
![](https://img.aspxhome.com/file/2023/4/68974_0s.jpg)
树型结构在ASP中的简单解决
2007-10-07 12:52:00