python小小爬虫(一)—— 爬取学校官网通知(仅作为练习使用)

无聊拿来玩儿,仅作为练习用。

文章目录

分析

找到文本的所处的div即可,注意应该是文本(一般是<span>text</span>这种形式)的上一级div,即div下面应该就是文本所在的span,找到文本所在的tag之后,使用.get_text()获取其文本信息。

代码

import requests  
from bs4 import BeautifulSoup  
  
# 获取html文档  
def get_html(url):  
    """get the content of the url"""  
    response = requests.get(url)  
    response.encoding = 'utf-8'  #中文乱码的话,可以试一下gb2312
    return response.text  
      
# 获取内容
def get_certain_web(html):  
    """get the content of the html"""  
    global soup  #方便调试
    soup = BeautifulSoup(html, 'lxml')  #使用lxml解析器对html进行解析,生成soup结构化文件
    web_content_temp = soup.select('div.index-tab-notice-right-list-title')#根据所需内容进行筛选
    web_content = ''
    for i in web_content_temp:
        web_content += i.get_text() + '\n'
    #web_content = soup.find('div',{'class':'zzj_5b_2d'})[0].get_text()    也可以使用这个
    return web_content

url_web = "http://www.zzu.edu.cn/" 
html = get_html(url_web)  
web_content = get_certain_web(html)  
print(web_content)  

效果

在这里插入图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值