import requests
from bs4 import BeautifulSoup
for i in range(1,6):
url = 'https://bj.lianjia.com/ditiezufang/rp%s/'%(i)
response = requests.get(url)
# with open('lianjia.html','wb') as f:
# f.write(response.content)
# html = response.text
# print(html)
soup = BeautifulSoup(response.text, 'lxml')
ul_tag = soup.find('ul', id="house-lst")
# print(ul_tag)
li_tags = ul_tag.find_all('li')
# print(li_tags)
for li_tag in li_tags:
# print(li_tag)
title = li_tag.select('div.info-panel > h2 > a')[0].text
print(title)
info = li_tag.select('div.where')[0].text
print(info)
info_lou = li_tag.select('div.con')[0].text
print(info_lou)
tags = li_tag.select('div.view-label')[0].text
print(tags)
price = li_tag.select('div.price')[0].text
print(price)
update_time = li_tag.select('div.price-pre')[0].text
print(update_time)
# page_num = li_tag.select('div.list-wrap > div > a:nth-child')[0].text
# print(page_num)
print('-'*50)
# body > div.wrapper > div.main - box.clear > div > div.list - wrap > div > a: nth - child(5)
链家(beautifulsoup)
最新推荐文章于 2021-10-30 17:16:10 发布