3.3 beautifulsoup4库
3.3.1 案例引入——爬取Q房租房网站的数据
爬取Q房租房网站的内容(网址https://beijing.qfang.com/rent),利用
beautifulsoup4库解析网页后,输出爬取到的第一个房源标题。
【例3-5】
【参考代码】
import requests #导入requests模块
from bs4 import BeautifulSoup #导入BeautifulSoup模块
url = 'https://beijing.qfang.com/rent' #定义url字符串
#发送请求,并将返回结果赋值给r
r = requests.get(url)
#创建BeautifulSoup对象,并设置使用lxml解析器
soup = BeautifulSoup(r.text, 'lxml')
#获取第一个房源标题,并赋值给title
title=soup.find(
'div', class_='list-main-header clearfix'
).a.string.strip()
print('第一个房源标题:', title) #输出title
评论0
最新资源