怎么提取全部标签https://sz.fang.lianjia.com/loupan/pg1
使用bs4解析,用字符串拼接和列表添加 。
import requests
from bs4 import BeautifulSoup as bs
res=requests.get('https://sz.fang.lianjia.com/loupan/pg1',headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36 Edg/96.0.1054.53'})
res.encoding='utf-8'
soup=bs(res.text,'lxml')
tags=[a.text.strip().replace('\n',';').split(';') for a in soup.select('div.resblock-tag')]
mlen=max([len(x) for x in tags])
tags=[x+['']*(mlen-len(x))for x in tags]
print(tags)
运行结果
[['品牌房企', 'VR看房', '绿化率高', ''], ['多轨交汇', '开发区', '成熟商圈', '视频看房'], ['VR看房', '绿化率高', '小型社区', '新盘首开'], ['多轨交汇', '
近主干道', '成熟商圈', '早教机构'], ['3D楼书', '品牌房企', '绿化率高', '大型社区'], ['多轨交汇', '成熟商圈', '配套齐全', '医疗配套'], ['公交直达', '多轨
交汇', '配套齐全', '成熟商圈'], ['公交直达', '多轨交汇', '配套齐全', '购物方便'], ['地铁沿线', '配套齐全', '购物方便', '医疗配套'], ['低单价', '地铁沿线
', '成熟商圈', '配套齐全']]
如有帮助,请点采纳。