利用Python中的BeautifulSoup库爬取安居客第一页信息

题目:
网址为https://beijing.anjuke.com/sale/,
利用BeautifulSoup库,爬取第1页的信息,具体信息如下:进入每个房源的页面,爬取小区名称、参考预算、发布时间和核心卖点,并将它们打印出来。(刚学网络爬虫。若有错误,望指正)
代码如下:

import requests
from bs4 import BeautifulSoup
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50'
}

info_lists = []

house=requests.get("https://beijing.anjuke.com/sale/",headers=headers)
soup=BeautifulSoup(house.text,"lxml")
names=soup.select("h3")
positions=soup.select("p.property-content-info-comm-name")
moneys=soup.select("div.property-price > p.property-price-total > span.property-price-total-num")
years=soup.select("div.property-content > div.property-content-detail > section > div:nth-of-type(1) > p:nth-of-type(5)")
points=soup.select("div.property-content > div.property-content-detail > section > div:nth-of-type(3)")



for name,position,money,year,point in zip(names,positions,moneys,years,points):
    info = {
        'name':name.get_text().strip(),
        'position':position.get_text().strip(),
        'money':money.get_text().strip(),
        'year':year.get_text().strip(),
        'point':point.get_text().strip()
    }
    info_lists.append(info)
    
for info_list in info_lists:
    f = open(r'C:\Users\23993\Desktop\house_info.txt','a+')
    try:
        f.write(info_list["name"]+'  '+info_list["position"]+'  '+info_list["money"]+'万'+'  '+info_list["year"]+'  '+info_list["point"]+'\n')
        f.close()
    except UnicodeEncodeError:
        pass

部分结果截图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值