zoukankan      html  css  js  c++  java
  • 爬取校园新闻首页的新闻

    1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文、show-info。

    2. 分析info字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。

    import requests
    newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res = requests.get(newsurl) #返回response对象
    res.encoding='utf-8'
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(res.text,'html.parser')
    #循环遍历打印
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
            t=news.select('.news-list-title')[0].text
            d=news.select('.news-list-description')[0].text
            a=news.a.attrs['href']
            print(t,d,a)
    # 取出一条新闻的标题、链接、发布时间、来源
    print('标题:'+soup.select('.news-list-title')[0].text)
    print('链接:'+soup.select('a')[2]['href'])
    print('发布时间:'+soup.select('.news-list-info')[0].span.text)
    print('来源:'+soup.select('.news-list-info')[0].select('span')[1].text)

    结果如下:

  • 相关阅读:
    vbs下载文件
    变量名自动变化
    VBS获得随机数,截图函数
    VBS定时关闭的弹窗
    VBS操作剪切板
    手动关闭端口
    win7,xp通用的打开文件浏览对话框的方法
    QTP全选页面的复选框
    SVN的使用
    工作中用到的前端内容整理
  • 原文地址:https://www.cnblogs.com/1103a/p/8718247.html
Copyright © 2011-2022 走看看