栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用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:Users23993Desktophouse_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

部分结果截图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/341787.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号