beautifulsoup爬取安居房信息

import requests
from bs4 import BeautifulSoup
headers={'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}
link="https://beijing.anjuke.com/sale/"
r=requests.get(link,headers=headers)
soup=BeautifulSoup(r.text,'lxml')
house_list=soup.find_all('li',class_="list-item")
i=0
for house in house_list:
    i=i+1

    name=house.find('div',class_="house-title").a.text.strip()
    price=house.find('span',class_="price-det").text.strip()
    price_unit=house.find('span',class_="unit-price").text.strip()
    room=house.find('div',class_="details-item").contents[1].text.strip()
    area=house.find('div',class_="details-item").contents[3].text.strip()
    high = house.find('div', class_="details-item").contents[5].text.strip()
    year = house.find('div', class_="details-item").contents[7].text.strip()
    broker=house.find('span',class_="broker-name").text.strip()
    address=house.find('span',class_="comm-address").text
    address=address.replace('\xa0\xa0\n                    ',' ').strip()
    tag_list=house.find_all('span',class_="item-tags")
    tags=[i.text for i in tag_list]
    print(i,name,price,price_unit,area,high,year,broker,address,tags)




 

你可能感兴趣的:(爬虫)