import bs4,requests,webbrowser

#定义存储匹配路径的列表
linkElems = []
print('Gooling......')

#输入查找的关键词
keyWord = input('Enter KEYWORD:')

#拼接下载网页的路径
res = requests.get('http://esf.zz.fang.com/house/c61-kw' + str(keyWord))

#检查是否下载成功
try:
    res.raise_for_status()
except Exception as exc:
    print('There was a problem:%s'%(exc))

#匹配查找出前5条记录的路径,并且拼接成完整路径存储在路径列表中
for i in range(1,6):
    selectElems = 'dl#list_D03_0'+str(i)+' a'
    bs4Soup = bs4.BeautifulSoup(res.text)
    Soup = bs4Soup.select(selectElems)
    linkElems.append('http://esf.zz.fang.com/'+str(Soup[0].get('href')))


#循环打开下载保存的网页
for i in range(len(linkElems)):
    webbrowser.open(linkElems[i])
    


本文转载:CSDN博客