网址:苏州短租房,苏州日租房,苏州短租公寓-游天下苏州短租站
导入库
from bs4 import BeautifulSoup
import requests
请求网页
url = 'http://www.youtx.com/suzhou/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'}
res = requests.get(url,headers=headers)
res.status_code
html=res.text
soup=BeautifulSoup(html,'lxml')
soup
soup.find_all('div',attrs={'class':'houseInfo clearfix'})
爬取一家店铺
one=soup.find_all('div',attrs={'class':'house-tit clearfix'})[0]
one
#名称
name=(''.join(one.find('a').contents)) #contents为列表需用(''.join)转为字符串
name #方法1
one.find('a').attrs['title'] #方法2
#